2013-07-20 4 views
0

숙제, 실험실, 테스트 등을 위해 사용자 입력을 받아들이는 클래스에 대해 프로그램을 작성해야하며,이를 문자열에 넣고이를 분리하는 별도의 클래스가 있습니다 별도의 "이중"숫자로 작성하고 writeToFile 메서드를 사용하여 학생의 점수를 파일에 기록합니다.do while 루프를 사용할 때 nextLine을 건너 뛰는 이유

나는 문제가 계속 발생한다. 사용자에게 처음으로 이름을 묻는다면 잘 동작하지만 사용자가 do-while 루프를 다시 입력하기로 결정하면 "당신의 이름은 무엇인가?"입니다. 건너 뛰고 이드에게 똑바로 간다. 마지막 입력이 문자열이 아닌 것과 관련이 있다는 것을 알고 있으며 그 해결책은 "keyboard.nextLine();"을 넣는 것입니다. 줄 바꿈을 소비하지 않습니다

... 
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): "); 
    answer=keyboard.nextInt(); 

} while(answer==1); 

: 질문 위,하지만

import java.util.Scanner; 
    import java.io.*; 
    import java.text.DecimalFormat; 

    public class GradeApplication 
    { 
     public static void main(String[] args) throws IOException 
     { 
      Scanner keyboard = new Scanner(System.in); 

      //Define variables 
      String name; 
      int id; 
      String homework; 
      String labs; 
      String tests; 
      double project; 
      double discussion; 
      int answer; 

      do 
      { 
       System.out.print("\nWhat is your name? "); 
       name=keyboard.nextLine(); 

       System.out.print("\nWhat is your student ID? "); 
       id=keyboard.nextInt(); 

       homework = keyboard.nextLine(); 
       System.out.println("\nPlease enter homework grades separated by spaces:"); 
       homework = keyboard.nextLine(); 

       System.out.println("Please enter lab grades separated by spaces:"); 
       labs = keyboard.nextLine(); 

       System.out.println("Please enter test grades separated by spaces:"); 
       tests = keyboard.nextLine(); 

       System.out.println("Please enter project grade:"); 
       project = keyboard.nextDouble(); 

       System.out.println("Please enter discussion grade:"); 
       discussion = keyboard.nextDouble(); 

       System.out.println("\nResults: "); 
       //Call toString method 
       Student_Khazma s = new Student_Khazma(name,id,homework,labs,tests,project,discussion); 
       System.out.print(s.toString()); 

    //Open file   
     PrintWriter outputFile= new PrintWriter("gradeReport.txt"); 
     System.out.println(s.writeToFile()); 
     outputFile.close(); 

     System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): "); 
     answer=keyboard.nextInt(); 

      System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): "); 
      answer=keyboard.nextInt(); 

       }while(answer==1); 

     } 
    } 

답변

6

귀하의 마지막 호출 Scanner#nextInt()에 .. 나는 그것을 시도하고 질문하기도 전에 그것은 단지 자신의 이름에 대한 사용자 요청 첫 번째 호출 인 Scanner#nextLine()으로 전달됩니다. 결과적으로 스캔 작업은 입력 대기를 차단하지 않습니다 (자세한 내용은 javadoc 참조). 하는 것이 당신이 keyboard.nextLine();을 추가해야합니다 해결 : nextLine()에 첫 번째 호출이 입력 (시 루프 다시 시작)를 차단합니다

... 
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): "); 
    answer=keyboard.nextInt(); 
    keyboard.nextLine(); // <== line added here 
} while(answer==1); 

그래야.

+0

완벽한 감사 :) –

관련 문제