0

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

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 和 ncolumns 的输出是 1 和 81。所以有人可以帮我找出正确的方法来计算列以确保有 9 个。或者它会最好开始比较行和列的值,以查看是否有任何重复项(1-9)在使用该错误检查以查看是否有正确的行数和列数?

4

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
于 2012-02-16T10:50:19.207 回答