나는 다음과 같은 코드를 사용하고 스캐너 in.hasNextInt()를 사용하여 무한 루프 :. 나는 'w'가 말해 것 "사용자가 입력 한 잘못된 입력을 입력하면, 그러나자바 :
while (invalidInput)
{
// ask the user to specify a number to update the times by
System.out.print("Specify an integer between 0 and 5: ");
if (in.hasNextInt())
{
// get the update value
updateValue = in.nextInt();
// check to see if it was within range
if (updateValue >= 0 && updateValue <= 5)
{
invalidInput = false;
}
else
{
System.out.println("You have not entered a number between 0 and 5. Try again.");
}
} else
{
System.out.println("You have entered an invalid input. Try again.");
}
}
을 다시 시도 . " "0에서 5 사이의 정수 지정 : 잘못된 입력을 입력했습니다. 다시 시도하십시오."라는 텍스트를 보여주는 무한 루프로 이동합니다.
왜 이런 일이 발생합니까? 프로그램이 명령문에 도달 할 때마다 사용자가 입력하고 Enter 키를 누를 때까지 기다리지 않아야합니까?
if (in.hasNextInt())
'in'이 무엇인지 또는 어떻게 구성했는지 아무에게도 보여주지 않았기 때문에 어떻게 행동해야하는지 알려주지 않습니다! –