【发布时间】:2011-04-11 17:37:00
【问题描述】:
未处理的异常:System.IndexOutOfRangeException:索引超出了数组的边界(在第一个 if 语句中)
static int arrayRows = 20;
static int arrayCols = 20;
public void printBoard()
{
int neighbours;
for (y = 0; y < arrayCols; y++)
for (x = 0; x < arrayRows; x++)
{
neighbours = 0;
// Count number of neighbours surrounding live cell
if (parentGen[x-1, y-1] == 1) // top left
neighbours++;
if (parentGen[x-1, y] == 1) // left
neighbours++;
if (parentGen[x-1, y+1] == 1) // bottom left
neighbours++;
if (parentGen[x, y-1] == 1) // middle top
neighbours++;
if (parentGen[x, y+1] == 1) // middle bottom
neighbours++;
if (parentGen[x+1, y-1] == 1) // top right
neighbours++;
if (parentGen[x+1, y] == 1) // right
neighbours++;
if (parentGen[x+1, y+1] == 1) // bottom right
neighbours++;
}
}
我唯一能想到的是我的程序正在检查
【问题讨论】:
-
请添加 parentGen 的声明以确保答案最有帮助。
-
int[,] parentGen = new int[arrayRows, arrayCols];
标签: c# indexoutofrangeexception