-1

尝试为数独板构建数据结构,我想将其打印到控制台以供初学者使用。我正在使用一个二维数组来表示数独游戏中的每个单元格,并且我在打印它时遇到了一个非常棘手的问题。

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( "" );
    }
}

}

4

3 回答 3

2

您没有为数组指定大小。

int[][] puzzleBoard = {};应该int[][] puzzleBoard = new int[10][10];

如果只用 0 填充数组,则无需使用 for 循环或逐行初始化数组。

然后数组是 0 基索引。所以第一个元素将在 position[0][0]和最后一个在 position [9][9]

您还可以删除两个变量rowscolumns.

最后,您应该将 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 中数组的更多信息,我建议您阅读.

于 2013-10-26T15:20:28.427 回答
2

您还应该使用 for 循环初始化数组元素!

另外,请记住,Java 数组是从零开始的,所以最上面的单元格是puzzleboard[0][0]!

您还应该为数组指定大小 -int[][] puzzleBoard = new int[10][10];

于 2013-10-26T15:21:43.043 回答
0

您应该使用java.util.Arrays.deepToString()打印二维数组:

返回指定数组的“深层内容”的字符串表示形式。如果数组包含其他数组作为元素,则字符串表示包含它们的内容等等。此方法设计用于将多维数组转换为字符串。

字符串表示由数组元素的列表组成,括在方括号 ("[]") 中。相邻元素由字符“,”(逗号后跟一个空格)分隔。元素通过 String.valueOf(Object) 转换为字符串,除非它们本身是数组。

于 2013-10-26T15:23:49.617 回答