如何创建子网格&数独程序Java中的分隔符?

时间:2015-05-21 14:30:31

标签: java

我正在创建一个Sudoku帮助程序,我帮助用户解决这个难题。

如何创建水平“=”和垂直“|”分隔符以将网格分解为子网格?对于水平分隔线,对于外(行)循环,在第2行和第5行之后添加分界线。对于垂直分隔线,内部(cols)循环,在第2列和第5列之后添加竖线“|”。

最后,如何为每个行和列创建标题,以便用户通过其ID识别每一行和列?

public class Sudoku {
   public static int rows = 9;
   public static int cols = 9;
   public static int[][] board = new int[rows][cols];

   public static void show() {
   for (int rows=1; rows<board.length; rows++) {
         for (int cols=1; cols<board.length; cols++) {
         board[rows][cols] = '0';
      }
   } 
}
   public static void main(String[] args) {
   //print out the contents of board array
      int board[][] = new int[9][9];

      for (int rows = 0; rows < 9; rows++) {
         for (int cols = 0; cols < 9; cols++) {
         System.out.print(board [rows][cols] + " ");
         }

         System.out.println("");
      }
      show();


   }
}

我的程序输出:

0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0

2 个答案:

答案 0 :(得分:1)

这是我前一段时间写的,只需将sudoku数组更改为你的arrayname(board)。

for(int x = 0; x < 9; x++){
        System.out.print("|     |     |     ||     |     |     ||     |     |     |\n");
        for(int y = 0; y < 9; y++){
            if(y == 0 || y == 3 || y == 6) System.out.print("|");
            System.out.print("  " + sudoku[x][y] + "  |");
        }
        System.out.print("\n");     
        System.out.print("|     |     |     ||     |     |     ||     |     |     |\n");
        if(x == 2 || x == 5)
            System.out.print("=========================================================");
        else
            System.out.print("---------------------------------------------------------");
        System.out.print("\n");
    }

这将创建如下内容:

---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  0  ||  0  |  0  |  0  ||  0  |  0  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  8  ||  0  |  6  |  5  ||  0  |  0  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  3  |  0  ||  9  |  0  |  4  ||  5  |  0  |  8  |
|     |     |     ||     |     |     ||     |     |     |
=========================================================
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  0  ||  0  |  2  |  0  ||  4  |  5  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  0  ||  0  |  0  |  0  ||  0  |  0  |  6  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  7  ||  0  |  0  |  0  ||  3  |  0  |  0  |
|     |     |     ||     |     |     ||     |     |     |
=========================================================
|     |     |     ||     |     |     ||     |     |     |
|  1  |  2  |  0  ||  0  |  4  |  6  ||  0  |  7  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  0  |  0  |  3  ||  0  |  8  |  0  ||  1  |  0  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------
|     |     |     ||     |     |     ||     |     |     |
|  6  |  0  |  0  ||  7  |  5  |  0  ||  0  |  0  |  0  |
|     |     |     ||     |     |     ||     |     |     |
---------------------------------------------------------

答案 1 :(得分:1)

有点难看,但你绝对可以改善它:

public static void main(String[] args) {
  //print out the contents of board array
  int board[][] = new int[9][9];
  System.out.println("  0 1 2 | 3 4 5 | 6 7 8 <-- column id");      
  System.out.println("========================  v- row id");
  for (int rows = 0; rows < 9; rows++) {
     for (int cols = 0; cols < 9; cols++) {    
         if (cols % 3 == 0) {
             System.out.print("| ");
         }
        System.out.print(board [rows][cols] + " ");            
     }         

     System.out.println("| "+ rows);
     if ((rows+1) %3 == 0) {
         System.out.println("========================");
     } 
  }
  show();


}

它显示了类似的东西:

  0 1 2 | 3 4 5 | 6 7 8 <-- column id
========================  v- row id
| 0 0 0 | 0 0 0 | 0 0 0 | 0
| 0 0 0 | 0 0 0 | 0 0 0 | 1
| 0 0 0 | 0 0 0 | 0 0 0 | 2
========================
| 0 0 0 | 0 0 0 | 0 0 0 | 3
| 0 0 0 | 0 0 0 | 0 0 0 | 4
| 0 0 0 | 0 0 0 | 0 0 0 | 5
========================
| 0 0 0 | 0 0 0 | 0 0 0 | 6
| 0 0 0 | 0 0 0 | 0 0 0 | 7
| 0 0 0 | 0 0 0 | 0 0 0 | 8
========================

更新:如果您希望行ID位于每行的开头,请执行以下操作:

public static void main(String[] args) {
    //print out the contents of board array
    int board[][] = new int[9][9];
    System.out.println("   0 1 2   3 4 5   6 7 8");      
    System.out.println("  ========================");
    for (int rows = 0; rows < 9; rows++) {
          System.out.print(rows+" |");
         for (int cols = 0; cols < 9; cols++) {             
            System.out.print(board [rows][cols] + " ");            
            if ((cols+1) % 3 == 0) {
                 System.out.print("| ");
            }
         }         

         System.out.println();
         if ((rows+1) %3 == 0) {
             System.out.println("  ========================");
         } 
     }
     show();
}

它会显示:

   0 1 2   3 4 5   6 7 8
  ========================
0 |0 0 0 | 0 0 0 | 0 0 0 | 
1 |0 0 0 | 0 0 0 | 0 0 0 | 
2 |0 0 0 | 0 0 0 | 0 0 0 | 
  ========================
3 |0 0 0 | 0 0 0 | 0 0 0 | 
4 |0 0 0 | 0 0 0 | 0 0 0 | 
5 |0 0 0 | 0 0 0 | 0 0 0 | 
  ========================
6 |0 0 0 | 0 0 0 | 0 0 0 | 
7 |0 0 0 | 0 0 0 | 0 0 0 | 
8 |0 0 0 | 0 0 0 | 0 0 0 | 
  ========================