我的任务是允许用户输入他们的彩票号码并检查是否 他们赢得了大奖。 用户应该有机会输入 4 个数字。每个数字应在 1-99 范围内。如果 用户输入一个小于1或大于99的数字,那么程序应该提示 他们输入正确范围内的数字。您应该使用 while 循环来确保用户输入 正确的数字。 while循环的退出条件应该是什么?
我尝试按照任务要求创建一个 while 循环。这不起作用。我完全陷入困境。
String password = "MyNameJeff";
Scanner dave = new Scanner(System.in);
System.out.println("lottery numbers");
int UserInput = dave.nextLine();
while (!password.equals(UserInput)) {
System.out.println random math command <----
UserInput = dave.nextLine();
}
System.out.println("Lottery numbers here?");
最佳答案
此 while
循环将获取第一个用户输入并检查其范围是否在 1 到 99 之间(包括 1 和 99)。
while(userInput < 1 || userInput > 99){
System.out.println("Please re-enter another lottery number: ");
userInput=dave.nextInt();
}
如果不在1到99范围内,将要求用户重新输入另一个彩票号码。
关于Java彩票机任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343679/