이 문제를 해결하기 위해 이상한 버그가있는 것 같습니다. 언제 .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
나는 수학이 잘못 이해하지만, 사람이 말해 줄 수 있다면 먼저 이름 부분을 파악해야 내가 뭘 크게 잘못 내가 할 일을 해요 고맙습니다.
(http://stackoverflow.com/questions/13102045/scanner-is-skipping [스캐너) (다음 nextInt() 또는 다른 nextFoo() 메소드를 사용 후 꽵()를 스킵한다] 봐주세요 -nextline-after-using-next-nextint-other-nextfoo) –
그 내용을 읽었습니다. 이해하지만, nextInt를 먼저 잡아 내지 못합니다. 그래서 주어진 첫 번째 문자열을 잘못 읽어야합니다. –