試圖建立一個數獨板的數據結構,並且我想將它打印到初學者的控制檯。我使用了一個2維數組來表示數獨遊戲中的每個單元格,並且我在打印時遇到了一個非常困難的問題。如何打印我的2維數組?
public class Main {
public static void main(String[] args) {
int[][] puzzleBoard = {};
//Row 1
puzzleBoard[1][1] = 0;
puzzleBoard[2][1] = 0;
puzzleBoard[3][1] = 0;
puzzleBoard[4][1] = 0;
puzzleBoard[5][1] = 0;
puzzleBoard[6][1] = 0;
puzzleBoard[7][1] = 0;
puzzleBoard[8][1] = 0;
puzzleBoard[9][1] = 0;
//Row 2
puzzleBoard[1][2] = 0;
puzzleBoard[2][2] = 0;
puzzleBoard[3][2] = 0;
puzzleBoard[4][2] = 0;
puzzleBoard[5][2] = 0;
puzzleBoard[6][2] = 0;
puzzleBoard[7][2] = 0;
puzzleBoard[8][2] = 0;
puzzleBoard[9][2] = 0;
//Row 3
puzzleBoard[1][3] = 0;
puzzleBoard[2][3] = 0;
puzzleBoard[3][3] = 0;
puzzleBoard[4][3] = 0;
puzzleBoard[5][3] = 0;
puzzleBoard[6][3] = 0;
puzzleBoard[7][3] = 0;
puzzleBoard[8][3] = 0;
puzzleBoard[9][3] = 0;
//Row 4
puzzleBoard[1][4] = 0;
puzzleBoard[2][4] = 0;
puzzleBoard[3][4] = 0;
puzzleBoard[4][4] = 0;
puzzleBoard[5][4] = 0;
puzzleBoard[6][4] = 0;
puzzleBoard[7][4] = 0;
puzzleBoard[8][4] = 0;
puzzleBoard[9][4] = 0;
//Row 5
puzzleBoard[1][5] = 0;
puzzleBoard[2][5] = 0;
puzzleBoard[3][5] = 0;
puzzleBoard[4][5] = 0;
puzzleBoard[5][5] = 0;
puzzleBoard[6][5] = 0;
puzzleBoard[7][5] = 0;
puzzleBoard[8][5] = 0;
puzzleBoard[9][5] = 0;
//Row 6
puzzleBoard[1][6] = 0;
puzzleBoard[2][6] = 0;
puzzleBoard[3][6] = 0;
puzzleBoard[4][6] = 0;
puzzleBoard[5][6] = 0;
puzzleBoard[6][6] = 0;
puzzleBoard[7][6] = 0;
puzzleBoard[8][6] = 0;
puzzleBoard[9][6] = 0;
//Row 7
puzzleBoard[1][7] = 0;
puzzleBoard[2][7] = 0;
puzzleBoard[3][7] = 0;
puzzleBoard[4][7] = 0;
puzzleBoard[5][7] = 0;
puzzleBoard[6][7] = 0;
puzzleBoard[7][7] = 0;
puzzleBoard[8][7] = 0;
puzzleBoard[9][7] = 0;
//Row 8
puzzleBoard[1][8] = 0;
puzzleBoard[2][8] = 0;
puzzleBoard[3][8] = 0;
puzzleBoard[4][8] = 0;
puzzleBoard[5][8] = 0;
puzzleBoard[6][8] = 0;
puzzleBoard[7][8] = 0;
puzzleBoard[8][8] = 0;
puzzleBoard[9][8] = 0;
//Row 9
puzzleBoard[1][9] = 0;
puzzleBoard[2][9] = 0;
puzzleBoard[3][9] = 0;
puzzleBoard[4][9] = 0;
puzzleBoard[5][9] = 0;
puzzleBoard[6][9] = 0;
puzzleBoard[7][9] = 0;
puzzleBoard[8][9] = 0;
puzzleBoard[9][9] = 0;
int rows = 9;
int columns = 9;
int i, j;
for (i = 1; i < rows; i++){
for(j = 1; j < columns; j++){
System.out.print(puzzleBoard[i][j] + " ");
}
System.out.println("");
}
}
}
哦,是的,正如在另一個答案中所說的,初始化計數器for循環爲0! – 2013-10-26 15:22:58