用Java打印数独矩阵 - 如何将输出输出到正确的格式

时间:2014-02-01 20:49:10

标签: java

这可能是一件非常容易的事,而且“我的上帝啊,它太棒了!”问题,但给定一个数独矩阵使用代码,如:

sudoku = new int[9][9];

public void printBoard() 
{
   for (int i=0; i<9; i++){
       for (int j=0; j<9; j++){
       if (sudoku[i][j] == 0) System.out.print(". "); 
       if (sudoku[i][j] == 1) System.out.print("1 ");
               if (sudoku[i][j] == 2) System.out.print("2 ");
               if (sudoku[i][j] == 3) System.out.print("3 ");
               if (sudoku[i][j] == 4) System.out.print("4 ");
               if (sudoku[i][j] == 5) System.out.print("5 ");
               if (sudoku[i][j] == 6) System.out.print("6 ");
               if (sudoku[i][j] == 7) System.out.print("7 ");
               if (sudoku[i][j] == 8) System.out.print("8 ");
               if (sudoku[i][j] == 9) System.out.print("9 ");
       }
       System.out.println(" ");
   }

}

我想要输出的内容是这样的:

. . .   9 . 2   6 . 4
6 . 4   3 . .   . 7 .
. 7 .   1 . 4   . . .

. . 3   . 1 9   8 . .
1 5 .   . 4 .   . 9 7
. . 7   8 2 .   3 . .

. . .   2 . 6   . 5 .
. 3 .   . . 7   1 . 2
9 . 2   5 . 1   . . .

就像我说的,我知道我错过了几行代码才能实现这一目标,但我会感激任何帮助!谢谢!

2 个答案:

答案 0 :(得分:0)

您获得了很多分数.,因为数组中的元素具有所有值0(默认情况下)。因此,因为条件:

if (sudoku[i][j] == 0) System.out.print(". ");

你只能获得积分。

修改要以所需格式打印,您可以尝试检查ij值是否为3的倍数:

for (int i = 0; i < 9; i++) {
    if (i % 3 == 0 && i != 0) // not first iteration
        System.out.println(); // print extra new-line each 3 iterations

    for (int j = 0; j < 9; j++) {
        if (j % 3 == 0) 
            System.out.print(" "); // to print spaces between columns

        if (sudoku[i][j] == 0) {
            System.out.print(". ");
            continue; // since it's 0, continue with next iteration
        }
        ... // here print values
    }
    System.out.println();
}

<强>输出:

 . . .  . . .  . . . 
 . . .  . . .  . . . 
 . . .  . . .  . . . 

 . . .  . . .  . . . 
 . . .  . . .  . . . 
 . . .  . . .  . . . 

 . . .  . . .  . . . 
 . . .  . . .  . . . 
 . . .  . . .  . . . 

答案 1 :(得分:0)

这是你需要做的:

package hts;


public class HTS {

static int[][] sudoku = new int[9][9];

public static void printBoard() {
    for (int i = 0; i < 9; i++) {
            System.out.print("\n");
            if(i%3==0)
                System.out.print("\n");
        for (int j = 0; j < 9; j++) {
            if (j % 3 == 0)
                System.out.print(" ");
            if (sudoku[i][j] == 0)
                System.out.print(". ");
            if (sudoku[i][j] == 1)
                System.out.print("1 ");
            if (sudoku[i][j] == 2)
                System.out.print("2 ");
            if (sudoku[i][j] == 3)
                System.out.print("3 ");
            if (sudoku[i][j] == 4)
                System.out.print("4 ");
            if (sudoku[i][j] == 5)
                System.out.print("5 ");
            if (sudoku[i][j] == 6)
                System.out.print("6 ");
            if (sudoku[i][j] == 7)
                System.out.print("7 ");
            if (sudoku[i][j] == 8)
                System.out.print("8 ");
            if (sudoku[i][j] == 9)
                System.out.print("9 ");
        }
    }
}

public static void main(String[] args) {
    printBoard();
}

}

输出:

. . .  . . .  . . . 
. . .  . . .  . . . 
. . .  . . .  . . . 

. . .  . . .  . . . 
. . .  . . .  . . . 
. . .  . . .  . . . 

. . .  . . .  . . . 
. . .  . . .  . . . 
. . .  . . .  . . .