异常处理导致infite循环(java)

时间:2017-08-06 07:21:44

标签: java exception-handling user-input

你好我是一个Java初学者。我想创建一个取1到9之间的整数的方法。我使用异常处理程序,以便它可以处理错误或不匹配的输入,但它似乎只是执行声明" choice = input.nextInt()"一次,因为它,我的循环变得无限。

代码如下:

import java.util.*;

public class Player{
    private int[] over;
    private int choice;
    private int coordinates[];
    private static Scanner input = new Scanner(System.in);


    public Player(){
        over = new int[5];
        for(int i = 0; i < 5; i++){
            over[i] = 1;
        }
        coordinates = new int[2];
        coordinates[0] = coordinates[1] = -1;
    }


    public void getChoice(){
            int choice = -1;
            boolean inputIsOk;
            do{
                System.out.print("Enter Your Choice: ");
                inputIsOk = true;
                try{
                    choice = input.nextInt();
                }
                catch(InputMismatchException e){
                    System.out.println("Invalid choice");
                    inputIsOk = false;
                }
                if(choice < 1 || choice > 9){
                    System.out.println("Enter Choice In Range(1-9)");
                    inputIsOk = false;
                }
            }while(!inputIsOk);
            System.out.println("You Entered "+choice);
    }
} 

这是测试类:

public class TestPlayer{
    public static void main(String args[]){
        Player p1 = new Player();
        p1.getChoice();
    }
}

这是输出: 第一种情况只输入整数选择

harsh@harsh-Inspiron-3558:~/java/oxgame$ javac TestPlayer.java 
harsh@harsh-Inspiron-3558:~/java/oxgame$ java TestPlayer 
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: -1
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice: 5
You Entered 5

当我输入错误的输入时,第二个:

Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice:g
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
and it goes on....

请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:1)

如果你改变如下的catch子句:

} catch (InputMismatchException e) {
    input.next();
    System.out.println("Invalid choice");
    inputIsOk = false;
}

这会有用,input.next();我不知道为什么, 旧代码 - 当你输入g-时只是执行这个choice = input.nextInt();,好像它仍然保持相同的值,它没有等待用户输入,调用next()修复了这个。

答案 1 :(得分:0)

这将有效

try{ 
       choice = Integer.parseInt(input.next());
   } 
catch(NumberFormatException e){
    System.out.println("Invalid choice");
    inputIsOk = false;
} 

原因是: Say Scanner从流中读取一个对象typeCache。在获得整数值之前,缓冲区不会被刷新,typeCache将保持String,直到使用next()(或任何等价物)读取它。

Scanner类代码:

    public int nextInt() {
    return nextInt(defaultRadix);
}

public int nextInt(int radix) {
    // Check cached result
    if ((typeCache != null) && (typeCache instanceof Integer)
        && this.radix == radix) {
        int val = ((Integer)typeCache).intValue();
        useTypeCache();
        return val;
    }.......

或者 只需在catch块中添加input.next();,它就会自动清除typeCache