这是我的变量
string[,] table = new string[104, 6];
int moveRow = 1;
int moveCol = 1;
int sum1 = 0;
int sum2 = 0;
int rowIndex1 = 1;
int rowIndex2 = 0;
int firstTableRow = 1;
int firstTableCol = 1;
int secondTableRow = 1;
int secondTableCol = 0;
int thirdTableRow = 0;
int thirdTableCol = 1;
int fourthTableRow = 0;
int fourthTableCol = 0;
int counterNextRow = 0;
现在它给了我(仅为我)一个奇怪的Array index is out of range
这里
if (moveRow > 1)
{
firstTableCol += 1;
secondTableCol += 1;
thirdTableCol += 1;
fourthTableCol += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol]; //<------- HERE ITS GIVING ME THE ERROR
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
moveRow += 1;
}
这是我的整个代码
for(int col = 0; col < table.GetLength(0); col++){
for (int row = 0; row < table.GetLength(1); row++)
{
//if not equal length BLUE[0] else RED[1]
//moverow = 1, movecol = 1 default
if (table[moveCol, moveRow] == null)
//1,1
{
//must be on table[2,0]
//scan
//3,2
string col1 = table[rowIndex1, row];
string col0 = table[rowIndex2, row];
if (col1 != null)
{
sum1++;
}
if (col0 != null)
{
sum2++;
}
counterNextRow += 1;
}
else
{
//if not null and on the first process
if (counterNextRow == 0)
{
//r1 = table[1,1] r2 = table[1,0] r3 = table[0,1] r4 = table[0,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
//hindi unang process
else
{
if (moveRow > 1)
{
firstTableCol += 1;
secondTableCol += 1;
thirdTableCol += 1;
fourthTableCol += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else if (d1 != null && d2 != null && d3 == null && d4 == null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
else
{
firstTableRow += 1;
secondTableRow += 1;
thirdTableRow += 1;
fourthTableRow += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
}
}
}
//condition that if 1st board column 1 and column 0 has an equal length
//mark on the 2nd board
if (sum1 == sum2)
{
//red[1]
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
//blue[0]
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
//increment everytime the column is finish scanning
sum1 = 0;
sum2 = 0;
rowIndex1++;
rowIndex2++;
moveCol++;
moveRow = 1;
}
}
由于这个错误,我一直被困在这里,我不知道为什么它会给我Array index is out of range
错误。我没有任何负数,所以据说我不会有那种错误。有人能指出我吗?
答案 0 :(得分:0)
数组索引范围必须为0到103而不是1到104.它可能会解决您的问题。
int moveRow = 0;
int moveCol = 0;
答案 1 :(得分:0)
您的moveRow
和moveCol
从1开始,您的for循环从0开始。因此,您的moveCol
比您的for循环索引器高一个。
答案 2 :(得分:0)
您的表格定义为char(0)
但您似乎正在编入索引[columns, rows]
答案 3 :(得分:0)
嗯,我忘了检查变量的长度到我正在使用的尺寸。所以通过这样做我的变量rowIndex1
,我需要像这样检查它
if(rowIndex1 + 1 < table.GetLength(0)){
rowIndex++;
}
当时感谢你们。