不知道如何阻止这种情况,同时循环无限重复。我用来检查用户输入是否是int。如果未输入 int,则循环将无限重复。hasNextInt
public static void validatingInput(){
Scanner scan = new Scanner(System.in);
boolean valid = false;
int userNumber = 0;
while(!valid) {
System.out.println("Enter number between 1 and 20: ");
if (scan.hasNextInt()) {
userNumber = scan.nextInt();
valid = true;
} else
System.out.print("Not an int. ");
}
}
沧海一幻觉
吃鸡游戏
相关分类