有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计算完成的数独板的行和列

所以我试图计算一个完整数独板的输入文件有多少行和列。我想出了这个循环

public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
    int nrows=0;
    int ncolumns=0;


   while(input1.hasNextLine()){

       while(input1.hasNextInt()){
           input1.nextInt();
           ncolumns++;
       }

        input1.nextLine();
        nrows++;
    }

    System.out.print("Rows: " +nrows + "\n");
    System.out.print("Columns: " +ncolumns + "\n");

    return new Pair<Integer, Integer>(nrows, ncolumns);

    }//end rowCoulmnCount

我是从以前的一个方法中得到这个方法的,在这个方法中,我读取并创建了数独板输入文件的数组。我想这两种方法是相似的,但它们不是。。nrws和NColumn的输出为1&;有人能帮我找出正确的方法来计算列数以确保有9列。或者最好开始比较行和列的值,以查看是否有任何重复项(1-9)正在使用,然后进行错误检查,以查看行和列的数量是否正确


共 (1) 个答案

  1. # 1 楼答案

    试试看:

    public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
        int nrows=0;
        int ncolumns=0;
    
    
       while(input1.hasNextLine()){
           Scanner subinput1 = new Scanner(input1.nextLine());
           while(subinput1.hasNextInt()){
               subinput1.nextInt();
               ncolumns++;
           }
    
            nrows++;
        }
    
        System.out.print("Rows: " +nrows + "\n");
        System.out.print("Columns: " +ncolumns + "\n");
    
        return new Pair<Integer, Integer>(nrows, ncolumns);
    
        }//end rowCoulmnCount