3
此代码检查用户是否输入它是有效的。如果它不是数字,它将继续循环,直到收到一个数字。之后它将检查该数字是否在界限内或小于界限。它将继续循环,直到它收到一个入站号码。但我的问题是,当我打印选择时,它只显示最后插入数字后的前一个数字。为什么这样?使用扫描仪扫描Java输入
public void askForDifficulty(){
System.out.println("Difficulty For This Question:\n1)Easy\n2)Medium\n3)Hard\nChoice: ");
int choice = 0;
boolean notValid = true;
boolean notInbound = true;
do{
while(!input.hasNextInt()){
System.out.println("Numbers Only!");
System.out.print("Try again: ");
input.nextLine();
}
notValid = false;
choice = input.nextInt();
}while(notValid);
do{
while(input.nextInt() > diff.length){
System.out.println("Out of bounds");
input.nextLine();
}
choice = input.nextInt();
notInbound = false;
}while(notInbound);
System.out.println(choice);
}
我想你的代码。第二个输入只有在我输入数字时才被读取2次 – KyelJmD
@KyelJmD哦,我看到了 - 你在那里还有另一个'nextInt',请参阅修正。 – dasblinkenlight
顺便说一句你也可以检查这个问题吗? http://stackoverflow.com/questions/12082557/java-scanner-validation-returning-the-second-input – KyelJmD