直接上代码:
省略无帮助的部分,假设这里需要强制输入的数据为int类型
Scanner scan = new Scanner(System.in);
while(!scan.hasNextInt())
{
//没有这一行的话会导致无限循环
String useless = scan.next();
System.out.println("不是数字,重新输入");
}
//当是数字时循环结束
int theNumber = scan.nextInt();
悟于:https://www.cnblogs.com/yin-tao/p/7326945.html