2012-02-16 2 views
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 &입니다. 누군가가 열을 계산하여 9가 있는지 확인하는 적절한 방법을 찾도록 도와 줄 수도 있습니다. 아니면 행과 열의 값을 비교하기 시작하여 사용중인 중복 (1-9 개)이 있는지 확인하십시오. 오류가 있으면 행과 열의 수가 정확한지 확인하십시오.

답변

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 
+0

내가 nextInt()가 그 다음 라인으로 이동하는 경우에도 다음의 정수를 얻을, 그게 문제라고 생각. 나는 그것을 지금 확인할 수 없다. –

+0

이것은 열을 계산하는 것을 제외하고는 거의 완벽하게 작동합니다. 코드를 계산하여 열을 계산할 수 있는지 알아볼 것입니다. 그러나 새 스캐너 하위 입력 1을 사용하는 목적이 무엇인지 물어볼 수 있습니까? –

+0

첫 번째 스캐너는 파일에서 라인을 읽으며 두 번째 스캐너는 라인에서 int를 읽습니다. –