我需要用户输入大于 10 的整数。
这是我的代码。
import java.util.*; //program uses class Scanner
public class Tests {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an Integer");
int weight;
do {
while (!input.hasNextInt()) {
System.out.println("Please enter an integer!");
input.next(); // this is important!
}
System.out.println("Enter an Integer >= 10");
weight = input.nextInt();
} while (weight < 10);
System.out.println("OK");
}
}
如果重量是整数,我的预期输出将是打印“OK”。
但我的实际输出是
Enter an Integer
20
Enter an Integer >= 10
OK
当条件满足时,我无法弄清楚如何摆脱“输入整数> = 10”。
仅在输入时打印消息not满足条件。
do {
while (!input.hasNextInt()) {
System.out.println("Please enter an integer!");
input.next(); // this is important!
}
weight = input.nextInt();
if ( weight < 10 ) {
System.out.println("Enter an Integer >= 10");
}
} while (weight < 10);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)