【发布时间】:2015-06-24 07:39:42
【问题描述】:
System.out.println("Enter the first test score:");
double test1 = input.nextInt();
if (!(test1 >= 0 && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a number between 0 and 100 .");
System.out.println("Enter the second test score:");
double test2 = input.nextInt();
if (!(test2 >= 0 && test2 <= 100))
System.out.println("This is out of the acceptable range, please enter a number between 0 and 100 .");
如果我提示用户输入 test1 的值及其在特定范围内的 nor,我如何再次提示他输入正确的值?现在如果我运行并输入一个错误的值“这超出了可接受的范围,请输入一个介于 0 和 100 之间的数字”消息会出现,但它会直接进入 test2。我试着这样做
System.out.println("Enter the first test score:");
double test1 = input.nextInt();
if (!(test1 >= 0 && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a number between 0 and 100 .");
double test1 = input.nextInt();
但我收到一条错误消息。 我是否必须使用循环,如果是这样怎么办?
【问题讨论】:
-
使用 while 循环,例如
while(!(test2 >= 0 && test2 <= 100)),不断请求新的输入。 -
but i get an error msg.那条错误消息说什么? -
循环并在不匹配时使用 continue 并在匹配时中断并到达 test2 条件。