我试图为我的问题找到解决方案,但找不到在实践中有效的解决方案。所以,如果你不确定你知道解决方案是什么,请不要回答。我真的需要具体的帮助。
问题是当我运行我的简单代码时 - 你可以选择一个数字,没关系,循环工作正常。当您选择 0 时,它也可以工作(运行完成),但是当您输入一个字母或任何字符串时 - 有一个问题......即使我尝试输入另一个值,异常也会不停地循环。
PS。我需要在这里使用扫描仪,所以请不要写读者等 - 只是如何解决这个特定问题。
干杯,
这是代码(仅主要):
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int dane = -1 ;
boolean keepLooping; //if you delete this works the same with same problem
do
{
keepLooping = false;
try
{
System.out.println("Pick a number:");
dane = sc.nextInt();
}catch (InputMismatchException e)
{
System.out.println("Your exception is: " + e);
keepLooping = false;
}
System.out.println("Out from exception");
}while ((dane != 0)||(keepLooping == true));
}