如何在控制台中打印2D char数组矩阵?

时间:2019-06-22 16:34:11

标签: c# arrays

我正在尝试创建一种绘制矩形的方法,

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

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

  

未处理的异常: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;
   }
}

1 个答案:

答案 0 :(得分:1)

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

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

然后,在某些循环中,您使用Height +4或Width +4在数组上进行写操作,但这会导致数组外的位置,因为数组从0开始,并且您使用了Height +4和Width数组构造函数为+4。

您的带有注释的代码:

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;
    }
}

输出:

enter image description here