我正在创建一个小算法,这是其中的一部分。
如果用户输入非整数值,我想输出一条消息,让用户再次输入一个数字:
boolean wenttocatch;
do
{
try
{
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
我得到了一个永无止境的循环,我不知道为什么。
如何识别用户是否输入了一些非整数?
如果用户输入了一个非整数,如何让用户重新输入?
更新
当我打印异常时,我得到“InputMismatchException”,我该怎么办?