尝试为数独板创建数据结构,我想将其打印到控制台以供初学者使用。我正在使用一个二维数组来表示数独游戏中的每个单元格,而且我有一个真正的nooby问题打印出来。
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( "" );
}
}
}
答案 0 :(得分:2)
您没有为数组指定大小。
int[][] puzzleBoard = {};
应为int[][] puzzleBoard = new int[10][10];
如果只填充0,则无需使用for循环或逐行初始化数组。
然后数组被0索引。因此,第一个元素位于[0][0]
位置,最后一个元素位于位置[9][9]
。
您还可以删除两个变量rows
和columns
。
最后,你应该修改你的for循环从0开始。
for (i = 0; i < puzzleBoard.length; i++){
for(j = 0; j < puzzleBoard[i].length; j++){
System.out.print(puzzleBoard[i][j] + " ");
}
System.out.println( "" );
}
要了解有关Java中数组的更多信息,建议您阅读this。
答案 1 :(得分:2)
您也应该使用for循环初始化数组元素!
另外,请记住,Java数组是基于零的,所以最顶层的单元格是puzzleboard[0][0]
!
您还应指定数组的大小 - int[][] puzzleBoard = new int[10][10];
答案 2 :(得分:0)
您应该使用java.util.Arrays.deepToString()
打印2D数组:
返回&#34;深度内容&#34;的字符串表示形式。的 指定的数组。如果数组包含其他数组作为元素,则 字符串表示包含其内容等。这种方法 用于将多维数组转换为字符串。
字符串表示由数组元素的列表组成, 用方括号括起来(&#34; []&#34;)。相邻的元素是分开的 人物&#34;,&#34; (逗号后跟空格)。元素是 通过String.valueOf(Object)转换为字符串,除非它们是 他们自己的数组。