姬長信(Redy)

java – 处理无限循环的异常


我的问题简短而甜蜜.我不明白为什么我的程序在捕获错误时无限循环.我做了一个新的try-catch语句,但它循环甚至复制,粘贴和修改了之前有效的程序中的相应变量.以下是声明本身及以下将是整个计划.谢谢您的帮助!
try {
    input = keyboard.nextInt();
}
catch(Exception e) {
    System.out.println("Error: invalid input");
again = true;

}
if (input >0 && input 

程序:

    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