사용자의 데이터를 읽는 중입니다 (시험 점수). 그런 다음 성적을 확인하고 있습니다. 그 후에 나는 성적을 표시하고있다. 그런 다음 사용자에게 다른 학년을 입력 할 것인지 묻습니다. 예라고 대답하면 다른 시험 점수를 입력하라고합니다. 그러나 여기에서는 try/catch를 사용하여 입력중인 데이터 유형의 유효성을 검사합니다. 그들이 정수가 아닌 다른 것을 입력하면 다시 루프를 시도하고 다른 등급을 입력하고 싶지만 디스플레이에 정수 오류 메시지를 입력하십시오. 그러나 여기 루프 프린트는 다른 학년을 두 번 입력하고 시험 점수를 두 번 올리면 이전 점수를 인쇄하고 다른 학년 진술을 두 번 입력하고 싶은 것을 인쇄합니다. 왜 이런 일이 발생합니까?루프가 잘못 인쇄되는 동안
import java.util.InputMismatchException;
import java.util.Scanner;
public class CatchingException {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score;
String choice;
boolean loop = true;
try {
System.out.println("Enter your percentage mark: ");
score = scan.nextInt();
do {
if(score <40) {
System.out.println("You FAILED");
}else if(score >=40 && score <50){
System.out.println("Your grade: PASS MARK");
}else if(score >=50 && score <60) {
System.out.println("Your grade: 2:2");
}else if (score >=60 && score <70) {
System.out.println("Your grade: 2:1");
}else {
System.out.println("Your grade: 1:1");
}
System.out.println("Do you want to enter another grade: ");
choice = scan.next();
if(choice.equalsIgnoreCase("yes")) {
System.out.println("Enter your percentage mark: ");
try{
score = scan.nextInt();
}catch(InputMismatchException e) {
System.err.println("Enter Integer");
loop = false;
}
}
}while(!loop);
}catch(InputMismatchException e) {
System.err.println("Incorrect Input ");
}
System.out.println("program terminated");
scan.close();
}
}
텍스트를 편집하십시오. 그것을 읽는 것은 거의 불가능합니다. – SklogW
@Pshemo 나는 OP가 키보드 버퍼에'nextInt'에 의해 끝나는 줄을 처리한다고 믿습니다. –
@ PM77-1'#nextInt()'는 공백을 신경 쓰지 않습니다. – Tom