我知道这段代码中有错误,但是我找不出来。player1.getId();返回的值是1,这点你要知道。我想打印出数组中值为1的元素的索引。在代码结尾处,我期望currentX为0,currentY为0,但它们都是9。希望能得到帮助。
int[][] grid = {
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
};
int currentX = 0;
int currentY = 0;
grid[0][0] = player1.getId();
grid[0][9] = 2;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == player1.getId());
{
currentX = i;
currentY = j;
}
System.out.print(grid[i][j]);
}
}
System.out.println();
System.out.println("Player1 is currently in row " + currentX + " and column " + currentY);