'Index was outside the bounds of the array' 改变二维整数值
'Index was outside the bounds of the array' changing 2D int values
我是 C# 的新手,我创建了一个 5x5 网格,将网格中的每个数字增加 1
,因此网格计数。
public static void Main () {
int width = 5;
int height = 5;
int gridNumber = 0;
int[,] grid = new int[height, width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (grid [x,y] + (gridNumber +1) < 10){
Console.Write (grid [x,y] + (gridNumber + 1) + " | " );
}
else if (grid [x,y] + (gridNumber +1) == 10) {
Console.Write (grid [x,y] + (gridNumber + 1) + " |" );
}
else {
Console.Write (grid [x,y] + (gridNumber + 1) + " | " );
}
gridNumber++;
}
Console.WriteLine ();
}
Console.ReadKey ();
}
我的问题是每当我更改网格尺寸(将 int
从 5
更改为其他任何内容)时,我都会收到一条错误消息 Index was outside the bounds of the array
。
int width = 5;
int height = 5;
关于如何解决这个问题有什么想法吗?
改变
int[,] grid = new int[height, width];
到
int[,] grid = new int[width, height];
我是 C# 的新手,我创建了一个 5x5 网格,将网格中的每个数字增加 1
,因此网格计数。
public static void Main () {
int width = 5;
int height = 5;
int gridNumber = 0;
int[,] grid = new int[height, width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (grid [x,y] + (gridNumber +1) < 10){
Console.Write (grid [x,y] + (gridNumber + 1) + " | " );
}
else if (grid [x,y] + (gridNumber +1) == 10) {
Console.Write (grid [x,y] + (gridNumber + 1) + " |" );
}
else {
Console.Write (grid [x,y] + (gridNumber + 1) + " | " );
}
gridNumber++;
}
Console.WriteLine ();
}
Console.ReadKey ();
}
我的问题是每当我更改网格尺寸(将 int
从 5
更改为其他任何内容)时,我都会收到一条错误消息 Index was outside the bounds of the array
。
int width = 5;
int height = 5;
关于如何解决这个问题有什么想法吗?
改变
int[,] grid = new int[height, width];
到
int[,] grid = new int[width, height];