So che sto facendo un errore da qualche parte in questo codice, ma non riesco a capirlo. Il player1.getId(); restituisce il valore 1 solo così sei consapevole. Sto cercando di stampare l'indice dell'array dove il valore è 1. Alla fine del codice, mi aspettavo che currentX fosse 0 e currentY fosse 0, ma erano entrambi 9. Qualsiasi aiuto sarebbe super.Java - Array 2d in loop per trovare l'indice di un valore non funzionante
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);
sono Vuoi griglia [0] .length nel vostro interno per, sembra che si sta dopo griglia [i] .length –