我正在创建一个小算法,这是其中的一部分。
如果用户输入非整数值,我想输出一条消息并让用户再次输入一个数字:
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);
Run Code Online (Sandbox Code Playgroud)
我得到了一个永无止境的循环,我不知道为什么。
如何识别用户是否输入了一些非整数?
如果用户输入一个非整数,我如何要求用户再次输入?
更新
当我打印异常时,我收到“InputMismatchException”,我该怎么办?
java input try-catch java.util.scanner inputmismatchexception