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

数独ボードの入力ファイルの配列を読み取って作成した以前の方法から、この方法を作成するというアイデアを得ました。2つの方法は似ていると思いましたが、そうではありません..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 に答える