我想使用0
计算数组中的零(countCellsToFill()
)数。
我尝试使用返回的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();
}
}
}
答案 0 :(得分:0)
for(int i=0; i<sudokuPuzzle.length; i++) {
for(int j=0; j<sudokuPuzzle[i].length; j++) {
if(sudokuPuzzle[i][j] == 0){
count++;
}
}
}
只需循环并计算0个单元格?
答案 1 :(得分:0)
public static int countCellsToFill(int[][] arr){
int count=0;
for(int[] r : arr){
for(int a: r){
if(a == 0){
count++;
}
}
}
return count;
}
最后的主要方法:
public static void main(String... args) throws Exception
{
//......
printSudoku(sudokuPuzzle);
int count = countCellsToFill(sudokuPuzzle);
System.out.println("Num of zeros: " + count);
}