2017-05-31 39 views
0

我想使用countCellsToFill()来计算阵列中零的数量(0)。 我试着循环与count+1这是返回。但它不会出现在输出中。有人请帮我完成这个。计算Sudoku中的零

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(); 
    }  
} 
} 
+1

你是什么意思 “使用countCellsToFill()”?您没有在您的问题中包含该方法,您的代码也不会在任何地方使用它。 – azurefrog

+0

@azurefrog countCellsToFill()还没有使用..我想用这种方法来计算零的数量..这里的零是为了在数独中的空格。我只是想计算细胞必须填充。 – Tippu

+0

@Tippu我在这里找不到任何'count + 1'? – Blasanka

回答

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); 
} 
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细胞?