作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个数独帮助程序,帮助用户解决难题。
如何创建水平“=”和垂直“|”分隔线以便将网格分解为子网格?对于水平分隔线,对于外部(行)循环,分隔线添加在第 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/
我是一名优秀的程序员,十分优秀!