我需要用一个while循环来询问用户一个介于1-100之间的数字,并告诉用户输入了错误的号码数字是负数或超过100.这是我迄今为止所拥有的。每当我运行它时,它都会询问用户的输入。当输入为负数或大于100时,表示无效号码,但当用户输入为45时,表示无效号码,当0-100之间的数字有效时。我不认为它读取代码的最后部分。怎么办请问用户输入正确的号码
import java.util.*;
public class PlayOffs {
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("What is the % chance Team 1 will win (1-99)?: ");
int team1input = scan.nextInt();
do {
while (team1input >= -1 || team1input >= 101) {
System.out.println("That's not between 1-99");
scan.next(); // this is important!
}
team1input = scan.nextInt();
} while (team1input >= 0 && team1input <= 100);
System.out.println("Thank you! Got " + team1input);
}
}
我真的不明白为什么你需要2个循环 –
此外,你的一些比较是不正确的 – theGleep
@FrankUnderwood内环可能处理案件提供像'富酒吧123'数据时。它会处理'foo'和'bar',然后接受'123'作为有效的号码。代码最有可能基于:[使用java.util.Scanner验证输入](https://stackoverflow.com/q/3059333) – Pshemo