java在尝试失败后关闭
我试图有一个输入,要求我选择一个数字1-99,我也想有这个系统。在两次尝试失败后关闭(0),如果有人能帮助我,将不胜感激
package joey;
import java.util.Scanner;
public class Joey {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Please enter an number between 0-99: ");
int n = input.nextInt();
if ((n > 99) || (n < 1))
System.out.println("Invalid number");
else
break;
}
}
}
# 1 楼答案
对失败的尝试使用计数器(代码更新):
看到了吗here
# 2 楼答案
您需要创建一个标志变量。。。比如:
然后将
while (true) {
循环更改为:不需要
System.exit(0);
呼叫或者你可以使用一个
do-while
循环,但我将由你决定# 3 楼答案
为了拥有“这个”系统。关闭时,只需手动完成程序运行,即退出循环。A.查一下你的电话号码。B.设置一个布尔标志,并使while循环在该标志上。当它改变状态时,循环将结束,计算机将返回到它的状态