任何人都可以告诉我为什么我会从此代码中收到System.IndexOutOfRangeException
吗?
char[,] matrix = new char[80, 18];
for (int i = 0; i < 80; i++)
for (int j = 0; i < 18; j++)
matrix[i, j] = '1';
答案 0 :(得分:11)
您正在检查第二个for循环中是否小于18
char[,] matrix = new char[80, 18];
for (int i = 0; i < 80; i++)
for (int j = 0; i < 18; j++) //<-- Right there.
matrix[i, j] = '1';
更改为:
char[,] matrix = new char[80, 18];
for (int i = 0; i < 80; i++)
for (int j = 0; j < 18; j++) //<-- Right there.
matrix[i, j] = '1';
答案 1 :(得分:4)
问题:,您正在使用变量i
而不是j
进行检查
for (int i = 0; i < 80; i++)
for (int j = 0; i < 18; j++)
^^^ should be j
试试这个:
for (int i = 0; i < 80; i++)
for (int j = 0; j < 18; j++)
答案 2 :(得分:3)
...因为你有错字 for(int j = 0; i &lt; 18; j ++)