您好,我是一个开始编程的学生,我正在练习使用循环来验证输入。不幸的是,循环工作但完全跳过了内部循环......我现在收到错误消息或提示......
这是我的代码:[我从本网站上关于验证输入的答案中借用了它,因此我可以对其进行测试。]
import java.util.Scanner;
public class ValidationTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.println("Please enter a positive number!");
while (!sc.hasNextInt())
{
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
} while (number <= 0);
System.out.println("Thank you! Got " + number);
}
}