2017-03-10 2 views
1

이 문제를 해결하기 위해 이상한 버그가있는 것 같습니다. 언제 .nextLine();을 사용하여 문자열을 저장하려고합니다. 새 줄을 만들고 두 번째 줄을 저장합니다.스캐너가 문자열을 올바르게 읽지 않습니다.

내 코드 : 공용 클래스 평균 {

public static void main (String args[]) { 

    String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    if(scr.nextLine().equalsIgnoreCase("quit")) { 
     System.exit(0); 
    } else { 
     studentName = scr.next(); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
}} 

오류가 저장되지 않는 첫 번째 입력하지만, 다른 라인에 가서 대신 저장합니다. 되는 경우가 출력됩니다 실행 :

학생들이

스택

오버플로

3

학생들의 점수를 입력

시험의 금액을 입력 이름을 입력

1

2

3

오버 플로우 2.0

나는 수학이 잘못 이해하지만, 사람이 말해 줄 수 있다면 먼저 이름 부분을 파악해야 내가 뭘 크게 잘못 내가 할 일을 해요 고맙습니다.

+0

(http://stackoverflow.com/questions/13102045/scanner-is-skipping [스캐너) (다음 nextInt() 또는 다른 nextFoo() 메소드를 사용 후 꽵()를 스킵한다] 봐주세요 -nextline-after-using-next-nextint-other-nextfoo) –

+0

그 내용을 읽었습니다. 이해하지만, nextInt를 먼저 잡아 내지 못합니다. 그래서 주어진 첫 번째 문자열을 잘못 읽어야합니다. –

답변

0

공공 정적 무효 메인 (문자열 인수 [])는 {

String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    String name = scr.nextLine(); 
    if (name.equalsIgnoreCase("quit")) 
    { 
     System.exit(0); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
} 

희망 유는 studentName에 사용자 입력을 지정하지 않았기 때문에 그것을

+0

* 희망 u 그것을 얻을 * 어쩌면 OP 얻을,하지만이 유형의 대답 초보자 또는이 코드에 익숙하지 않은 사람을 위해 매우 유용하지 않습니다. 당신이 한 일을 진술하십시오. –

+0

@ScaryWombat 글쎄 ... 여기에 설명이 있습니다 ... java.util.Scanner.nextLine() 메소드는이 스캐너를 현재 줄 앞으로 이동시키고 건너 뛴 입력을 반환합니다. 이 메소드는 끝에있는 행 구분자를 제외한 나머지 현재 행을 리턴합니다. 위치는 다음 행의 시작으로 설정됩니다. 따라서 Scanner.next()는 다음 항목을 기다릴 것입니다. 이것이 첫 번째 문자열을 건너 뛰는 이유입니다 ... – vineeth

1
문이 소비에 따라

(그러나 저장하지 않습니다) 첫 번째 이름 : 당신이 원하는 경우

if(scr.nextLine().equalsIgnoreCase("quit"))

studentName 변수에 저장됩니다 두 번째 이름은

studentName = scr.next();

입니다 첫 번째 이름도 저장하십시오. 먼저 변수에 저장해야합니다.

+1

여기 괜찮은 대답은 –

+0

@ SkaryWombat입니다. 격려의 말씀에 감사드립니다. 저에게 SO에 공헌하도록 동기를 유지하겠습니다.:) – VHS

0

오류 scr.nextLine().equalsIgnoreCase("quit")에 얻을.

아래 코드를 사용하고 검토하십시오.

public static void main (String args[]) { 

    String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    studentName=scr.nextLine(); 
    if(studentName.equalsIgnoreCase("quit")) { 
     System.exit(0); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
}} 
관련 문제