我正在处理一个家庭作业问题,必须为随机数生成生成种子。我正在使用do-while循环再次询问用户是否未输入数字(例如0或1)。在程序进入else部分时,我的循环似乎没有重置(它仅显示println)我不知道为什么吗?
byte seedChoice = 0;
do{
seedChoice=scanner.nextByte();
if(seedChoice == 0){
System.out.println("Please enter a seed for the random generation: ");
seed = scanner.nextLong();
}
else if (seedChoice == 1){
seed = 1970;
System.out.println("You are using the default 1970 seed.");
}
else{
System.out.println("You didn't enter a valid number.");
}
}while(seedChoice < 0 || seedChoice > 1);
扫描仪再次询问号码并重新开始循环的预期结果。
答案 0 :(得分:1)
您的代码正在等待更多输入。
提示的打印不在循环中,因此,当您输入无效的数字时,它将返回到等待更多输入而无需重新打印提示。
要解决此问题,只需将提示的打印内容移动到循环的正文中即可。