尝试捕捉JAVA的无尽循环

时间:2014-09-01 09:22:22

标签: java loops while-loop try-catch

所以我要做的是让用户在矩阵中输入一个有效坐标,即一个大于-1的INT,

Scanner scanner = new Scanner (System.in);
int coordinates[] = new int[2];
coordinates[0]=-1;  
coordinates[1]=-1;
boolean check = true;
while (((coordinates[0]<0)||(coordinates[0]>R))  && check) {
    System.out.print("Please enter a valid row number:\t");
    try { 
        coordinates[0]=scanner.nextInt(); 
        break; 
    }
    catch (InputMismatchException e) {
    }
}
while (((coordinates[1]<0)||(coordinates[1]>C)) && check) {
    System.out.print("Please enter a valid col number:\t");
    try { 
        coordinates[1]=scanner.nextInt(); 
        break; 
    }
    catch (InputMismatchException e) {
    }       
}

问题是在输入无效输入后它会无休止地循环 int R是行的大小 int C是collumn的大小

3 个答案:

答案 0 :(得分:1)

如果“无效输入”是指“不是任何整数”,那么每次尝试读取另一个整数时,您的扫描仪都会失败,因此您将点击catch,并且不执行任何操作停止循环。也许您打算在这种情况下将check设置为false?或许你打算将break放在每个catch

在读取有效整数时使用break是不对的,因为它可能是一个负整数,你的循环守卫说你不想要。

答案 1 :(得分:1)

您的问题是您没有处理您正在捕捉的错误。 如果您为nextInt()方法提供了错误的数字格式,则InputMismatchException将为thrown。然后因为catch什么都不做,循环将继续(从开始开始)并且扫描程序将读取相同的错误值,依此类推......

所以不要这样:

catch (InputMismatchException e) {
}  

试试这个:

catch (InputMismatchException e) {
    System.out.println("Wrong number entered.");
    scanner.nextLine();
}   

这样您就可以强制scanner移出最后一次错误输入。

修改

由于您在阅读输入后执行break,因此您的循环也会中断。在这种情况下,如果你输入负数,你也会break并且不会检查循环条件。删除break语句,它将按预期工作:

while (((coordinates[0]<0)||(coordinates[0]>R))  && check) {
    System.out.print("Please enter a valid row number:\t");
    try { 
        coordinates[0]=scanner.nextInt(); 
    }
    catch (InputMismatchException e) {
        System.out.println("That's not a valid number.");
        scanner.nextLine();
    }
}

<强> EDIT2:

public static void main(final String args[])
{
    int maxRowsNumber = 10;
    int maxColsNumber = 10;
    Scanner scanner = new Scanner (System.in);
    int coordinates[] = new int[2];
    coordinates[0]=-1;  
    coordinates[1]=-1;
    boolean check = true;
    while (((coordinates[0]<0)||(coordinates[0]>maxRowsNumber))  && check) {
        System.out.print("Please enter a valid row number:\t");
        try { 
            coordinates[0]=scanner.nextInt(); 
        }
        catch (InputMismatchException e) {
            System.out.println("That's not a valid number.");
            scanner.nextLine();
        }
    }
    while (((coordinates[1]<0)||(coordinates[1]>maxColsNumber)) && check) {
        System.out.print("Please enter a valid col number:\t");
        try { 
            coordinates[1]=scanner.nextInt(); 
        }
        catch (InputMismatchException e) {
            System.out.println("That's not a valid number.");
            scanner.nextLine();
        }       
    }
    System.out.println("Inserted RowsNumber: " + coordinates[0]);
    System.out.println("Inserted RowsNumber: " + coordinates[1]);
}

输出:

Please enter a valid row number:    11
Please enter a valid row number:    22
Please enter a valid row number:    10
Please enter a valid col number:    11
Please enter a valid col number:    2
Inserted RowsNumber: 10
Inserted RowsNumber: 2

答案 2 :(得分:0)

这与你的工作基本相同,我只是通过删除硬编码值,使变量更具描述性,并包含输入验证来尝试改进它。

final int ROW = 0;
final int COL = 1;

int coordinates[] = new int[2];
coordinates[ROW] = -1;
coordinates[COL] = -1;
boolean isInputValid = true;

Scanner scanner = new Scanner(System.in);
do {
    try {
        System.out.print("Please enter a valid row number:\t");
        coordinates[ROW] = Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException nfe) {
        isInputValid = false; //if the input is not int
    }
} while (!isInputValid && (coordinates[ROW] < 0) //do this until the input is an int
        || (coordinates[ROW] > R));              //and it's also not less than 0 or greater than R


//same logic applies here
do {
    try {
        System.out.print("Please enter a valid col number:\t");
        coordinates[COL] = Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException nfe) {
        isInputValid = false;
    }
} while (!isInputValid && (coordinates[COL] < 0)
        || (coordinates[COL] > C));

希望这有帮助。