计算数独中的零点
Count the zeros in Sudoku
我想使用 countCellsToFill()
计算数组中零 (0
) 的数量。
我尝试使用返回的 count+1
循环。但它不会出现在输出中。任何人都请帮助我完成这个。
public class Sudoku{
public static void main(String... args) throws Exception
{
Scanner scanner = new Scanner(System.in);
int[][] sudokuPuzzle = {
{8, 1, 0, 0, 0, 0, 0, 3, 9},
{0, 0, 0, 9, 0, 1, 0, 0, 0},
{3, 0, 5, 0, 0, 0, 4, 0, 1},
{0, 0, 9, 8, 0, 2, 7, 0, 0},
{0, 0, 0, 5, 0, 6, 0, 0, 0},
{0, 0, 4, 3, 0, 7, 1, 0, 0},
{1, 0, 8, 0, 0, 0, 9, 0, 2},
{0, 0, 0, 6, 0, 4, 0, 0, 0},
{2, 4, 0, 0, 0, 0, 0, 6, 5}
};
printSudoku(sudokuPuzzle);
}
public static void printSudoku(int[][] sudokuPuzzle)
{
for (int i = 0; i < sudokuPuzzle.length; i++)
{
if (i == 3 || i == 6)
System.out.println("------------------------");
for (int j = 0; j < sudokuPuzzle[i].length; j++)
{
System.out.format("%-2s", sudokuPuzzle[i][j]);
if (j == 2 || j == 5 )
System.out.print(" | ");
}
System.out.println();
}
}
}
for(int i=0; i<sudokuPuzzle.length; i++) {
for(int j=0; j<sudokuPuzzle[i].length; j++) {
if(sudokuPuzzle[i][j] == 0){
count++;
}
}
}
简单地遍历并计算 0 个单元格?
public static int countCellsToFill(int[][] arr){
int count=0;
for(int[] r : arr){
for(int a: r){
if(a == 0){
count++;
}
}
}
return count;
}
在最后的main方法中:
public static void main(String... args) throws Exception
{
//......
printSudoku(sudokuPuzzle);
int count = countCellsToFill(sudokuPuzzle);
System.out.println("Num of zeros: " + count);
}
我想使用 countCellsToFill()
计算数组中零 (0
) 的数量。
我尝试使用返回的 count+1
循环。但它不会出现在输出中。任何人都请帮助我完成这个。
public class Sudoku{
public static void main(String... args) throws Exception
{
Scanner scanner = new Scanner(System.in);
int[][] sudokuPuzzle = {
{8, 1, 0, 0, 0, 0, 0, 3, 9},
{0, 0, 0, 9, 0, 1, 0, 0, 0},
{3, 0, 5, 0, 0, 0, 4, 0, 1},
{0, 0, 9, 8, 0, 2, 7, 0, 0},
{0, 0, 0, 5, 0, 6, 0, 0, 0},
{0, 0, 4, 3, 0, 7, 1, 0, 0},
{1, 0, 8, 0, 0, 0, 9, 0, 2},
{0, 0, 0, 6, 0, 4, 0, 0, 0},
{2, 4, 0, 0, 0, 0, 0, 6, 5}
};
printSudoku(sudokuPuzzle);
}
public static void printSudoku(int[][] sudokuPuzzle)
{
for (int i = 0; i < sudokuPuzzle.length; i++)
{
if (i == 3 || i == 6)
System.out.println("------------------------");
for (int j = 0; j < sudokuPuzzle[i].length; j++)
{
System.out.format("%-2s", sudokuPuzzle[i][j]);
if (j == 2 || j == 5 )
System.out.print(" | ");
}
System.out.println();
}
}
}
for(int i=0; i<sudokuPuzzle.length; i++) {
for(int j=0; j<sudokuPuzzle[i].length; j++) {
if(sudokuPuzzle[i][j] == 0){
count++;
}
}
}
简单地遍历并计算 0 个单元格?
public static int countCellsToFill(int[][] arr){
int count=0;
for(int[] r : arr){
for(int a: r){
if(a == 0){
count++;
}
}
}
return count;
}
在最后的main方法中:
public static void main(String... args) throws Exception
{
//......
printSudoku(sudokuPuzzle);
int count = countCellsToFill(sudokuPuzzle);
System.out.println("Num of zeros: " + count);
}