2 回答
TA贡献1909条经验 获得超7个赞
我只是最终做了很长的路。
for(int c=0; c<=9; c++)//if number 1
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r-1]<20) Sprite.mine_2 [c][r] +=1; //top left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number2
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r ]<20) Sprite.mine_2 [c][r] +=1; //left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number3
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number4
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c ][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number5
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number6
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r ]<20) Sprite.mine_2 [c][r] +=1; // right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number7
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r-1]<20) Sprite.mine_2 [c][r] +=1; // top right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
TA贡献1840条经验 获得超5个赞
由于多级循环,此解决方案将非常耗时,但这仍然可以检查特定 c 和 r 值的所有情况......
for (int c = 0; c <= 9; c++)
{
for (int r = 0; c <= 9; r++)
{
for (int i = -1; c <= 1; i++)
{
for (int j = -1; c <= 1; j++)
{
try
{
if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )
{
Sprite.count++;
}
catch (Exception e)
{
// print what you want
}
}
}
}
}
对于更好的情况,请尝试这样的操作以避免 IndexOutOfBoundException 以 1 到 10 但实际数组为 0 - 11 开始 c 和 r
for (int c = 1; c <= 10; c++)
{
for (int r = 1; c <= 10; r++)
{
for (int i = -1; c <= 1; i++)
{
for (int j = -1; c <= 1; j++)
{
try
{
if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )
{
Sprite.count++;
}
catch (Exception e)
{
// print what you want
}
}
}
}
}
添加回答
举报