异常处理无限循环 [英] Exception handling infinite loop
问题描述
我的问题简短而有趣。我不明白为什么我的程序在捕获错误时会无限循环。我做了一个新的try-catch语句,但是它循环了,甚至复制,粘贴并修改了以前有效的程序中的适当变量。下面是语句本身,下面是整个程序。谢谢您的帮助!
my question is short and sweet. I do not understand why my program infinitely loops when catching an error. I made a fresh try-catch statement but it looped and even copied, pasted and modified the appropriate variables from a previous program that worked. Below is the statement itself and below that will be the entire program. Thank you for your help!
try {
input = keyboard.nextInt();
}
catch(Exception e) {
System.out.println("Error: invalid input");
again = true;
}
if (input >0 && input <=10)
again = false;
}
程序:
public class Blanco {
public static int input;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
nameInput();
}
/**
*
* @param name
*/
public static void nameInput() {
System.out.println("What is the name of the cartoon character : ");
Scanner keyboard = new Scanner(System.in);
CartoonStar star = new CartoonStar();
String name = keyboard.next();
star.setName(name);
typeInput(keyboard, star);
}
public static void typeInput(Scanner keyboard, CartoonStar star) {
boolean again = true;
while(again){
System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
+ "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");
try {
input = keyboard.nextInt();
}
catch(Exception e) {
System.out.println("Error: invalid input");
again = true;
}
if (input >0 && input <=10)
again = false;
}
switch (input) {
case 1:
star.setType(CartoonType.FOX);
break;
case 2:
star.setType(CartoonType.CHICKEN);
break;
case 3:
star.setType(CartoonType.RABBIT);
break;
case 4:
star.setType(CartoonType.MOUSE);
break;
case 5:
star.setType(CartoonType.DOG);
break;
case 6:
star.setType(CartoonType.CAT);
break;
case 7:
star.setType(CartoonType.BIRD);
break;
case 8:
star.setType(CartoonType.FISH);
break;
case 9:
star.setType(CartoonType.DUCK);
break;
case 10:
star.setType(CartoonType.RAT);
break;
}
popularityNumber(keyboard, star);
}
public static void popularityNumber(Scanner keyboard, CartoonStar star) {
System.out.println("What is the cartoon popularity number?");
int popularity = keyboard.nextInt();
star.setPopularityIndex(popularity);
System.out.println(star.getName() + star.getType() + star.getPopularityIndex());
}
}
推荐答案
您的程序将永远运行,因为在不更改扫描器状态的情况下调用 nextInt
会一次又一次导致异常:如果用户未输入 int
,调用 keyboard.nextInt()
不会更改扫描仪的外观,因此当您调用 keyboard.nextInt()
在下一次迭代中,您将获得一个异常。
Your program runs forever because calling nextInt
without changing the state of the scanner is going to cause an exception again and again: if the user did not enter an int
, calling keyboard.nextInt()
will not change what the scanner is looking at, so when you call keyboard.nextInt()
in the next iteration, you'll get an exception.
您需要添加一些代码以读取垃圾内容用户在处理异常以解决此问题后输入:
You need to add some code to read the garbage the user entered after servicing an exception to fix this problem:
try {
...
} catch(Exception e) {
System.out.println("Error: invalid input:" + e.getMessage());
again = true;
keyboard.next(); // Ignore whatever is entered
}
注意:您无需依赖异常在这种情况下:您可以调用 nextInt() java / util / Scanner.html#hasNextInt%28%29 rel = nofollow> hasNextInt()
,并检查扫描仪是否正在查看是否为整数。
Note: you do not need to rely on exceptions in this situation: rather than calling nextInt()
, you could call hasNextInt()
, and check if the scanner is looking at an integer or not.
这篇关于异常处理无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!