【问题标题】:Using InputMismatchException try/catch block使用 InputMismatchException try/catch 块
【发布时间】:2015-04-23 03:39:01
【问题描述】:

我想捕获任何不是 Int 的输入并提示输入有效输入 3 次。 3 次后,程序将继续并要求另一个评级。我似乎无法重复 try/catch 块,甚至无法捕获 InputMismatchException。有什么建议吗?

do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            rating = response.nextInt();
        } //end catch
    } while (rating >= 0);

【问题讨论】:

  • do-while 循环之外添加某种counter,初始化为0,在catch 块中,增加counter 并将rating 设置为@ 987654328@.

标签: java try-catch


【解决方案1】:

您可以循环执行:

int count = 0;
do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            count++;
        } //end catch
    } while (rating >= 0 && count < 3);

或者使用嵌套的 try/catch:

do {
    //prompt for rating
        try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
             try {
        System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
        //capture
            rating = response.nextInt();
        } catch (InputMismatchException err) {
            System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            rating = response.nextInt();
        } //end catch
        } //end catch
    } while (rating >= 0);

我个人更喜欢第一种方法。

我试过这段代码,它运行时没有任何异常:

公共类主{

public static void main(String[] args) throws IOException {
    int count = 0;
    int rating = 0;
    do {
        Scanner response = new Scanner(System.in);
        //prompt for rating
            try {
            System.out.println("Enter a rating between 0-4 for the Movie of the Week (Enter -1 to stop/exit): ");
            //capture
                rating = response.nextInt();

            } catch (InputMismatchException err) {
                System.out.println("Invalid input. Please enter a rating 0-4 or enter -1 to exit: ");
            } finally {
                count++;
                System.out.println("Rating-->" + rating);
            }
      } while (rating >= 0 && count < 3);

}

}

【讨论】:

  • 我的程序仍然抛出 InputMismatchException 错误,即使我发现它并提示另一个评级。关于为什么会这样的任何想法?
  • 我确实最终使用了第一个建议,但提示了 3 次。
  • 我刚试了一段代码,运行无异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2018-01-16
  • 2019-12-04
相关资源
最近更新 更多