首页 > 解决方案 > 如何在控制台中打印二维字符数组矩阵?

问题描述

我正在尝试创建一个像这样绘制矩形的方法:

--------------
|            |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
|            |
--------------

我想在控制台中显示整个数组矩阵,但是当我尝试运行程序时,我得到了这个错误:

未处理的异常:System.IndexOutOfRangeException:索引超出了数组的范围。

我试图解决这个问题几个小时,但找不到问题。这是我的代码:

public static class Canvas
{
    public static char[,] Draw (uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; ++i)
        {
            for (uint j = 1; j < height + 3; ++j)
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 5; ++i)
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 5; ++i)
        {
            page[height + 4, i] = '-';
        }
        for (uint j = 1; j < height + 4 ++j)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; ++j)
        {
            page[j, width + 4] = '|';
        }
        return page;
   }
}

标签: c#arrays

解决方案


首先,我不知道这是否是一个错字,但你的第四个 FOR 循环缺少一个“;” 在第二个参数之后。

其次,++i和i++是有区别的。在 for 循环中,通常使用 i++,因为它首先使用值,然后加 1。如果使用 ++i,它会先加 1,然后使用值。这会将您的计数器 (i) 扔出数组边界。

之后,在某些循环中,您使用 Height +4 或 Width +4 在数组上写入,但这会导致一个位置超出数组,因为数组从 0 开始,并且您使用 Height +4 和 Width +4 on数组构造函数。

您的带有注释的代码:

public static class Canvas
{


    public static char[,] Draw(uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; i++) //goes from 1 to 4 - 0 is null
        {
            for (uint j = 1; j < height + 3; j++) //goes from 1 to 4 - 5 is null
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 5; i++)  // goes from 0 to 6, but array ends at 5
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 5; i++) // goes from 0 to 6, but array ends at 5
        {
            page[height + 4, i] = '-'; //the argument "height +4" throw the position out of the array, because arrays starts at 0
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, width + 4] = '|'; //the argument "width +4" throw the position out of the array, because arrays starts at 0
        }
        return page;
    }
}

新代码:

    public static class Canvas2
{


    public static char[,] Draw(uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; i++) 
        {
            for (uint j = 1; j < height + 3; j++) 
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 4; i++) 
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 4; i++)
        {
            page[height + 3, i] = '-';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, width + 3] = '|';
        }
        return page;
    }
}

输出:

在此处输入图像描述


推荐阅读