-1

数独ボードのデータ構造を構築しようとしています。初心者向けにコンソールに出力したいと思います。数独ゲームで各セルを表すために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( "" );
    }
}

}

4

3 に答える 3

0

java.util.Arrays.deepToString()2D 配列を印刷 するために使用する必要があります。

指定された配列の「深い内容」の文字列表現を返します。配列に要素として他の配列が含まれている場合、文字列表現にはそれらの内容などが含まれます。このメソッドは、多次元配列を文字列に変換するために設計されています。

文字列表現は、角括弧 ("[]") で囲まれた配列の要素のリストで構成されます。隣接する要素は、文字 ", " (カンマの後にスペースが続く) で区切られます。要素は、それ自体が配列でない限り、String.valueOf(Object) によって文字列に変換されます。

于 2013-10-26T15:23:49.617 に答える