gpt4 book ai didi

java - 如何在数独程序 Java 中创建子网格和分隔线?

转载 作者:行者123 更新时间:2023-12-02 04:39:15 25 4
gpt4 key购买 nike

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

如何创建水平“=”和垂直“|”分隔线以便将网格分解为子网格?对于水平分隔线,对于外部(行)循环,分隔线添加在第 2 行和第 5 行之后。对于垂直分隔线,内部(列)循环,在第 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

最佳答案

这是我前段时间写的,只需将 sudoku 数组更改为您的数组名称(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 |
| | | || | | || | | |
---------------------------------------------------------

关于java - 如何在数独程序 Java 中创建子网格和分隔线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com