如何捕获多个异常并循环,直到我得到有效的输入?

我必须捕获3个异常并循环,直到输入有效的输入。


现在,我的程序在输入有效输入时结束,但只是连续循环无效输入。它永远不会捕获异常。如果我取出循环并输入一个“m”,它会捕获算术异常,但会打印输入不匹配“仅数字”的错误消息


// Get input from user and perform integer divsion.

do{

    try{

        System.out.print("Please enter a positive value for the numerator: ");

        numerator = keyboard.nextInt();        

        System.out.print("Please enter a positive value for the denominator: ");

        denominator = keyboard.nextInt();

    } 

    catch(InputMismatchException e){

          System.out.println("Enter only numbers.");

    }

    catch(NegativeValueException e){

          System.out.println("no negative values.");

    }

    catch(ArithmeticException e){

          System.out.println("Division by zero.");

    }


    if (numerator >=0 && denominator >= 0){

        quotient = numerator / denominator;

    }else if(numerator<0 && denominator <0){

        throw new NegativeValueException();

    }else

        invalidInput = true;


    System.out.println();

    System.out.print("The result of integer division is: ");

    System.out.println(quotient);

    System.out.println();

}while (invalidInput);  

我只需要捕获异常并让它打印出异常和循环的相应错误消息,直到输入有效输入。如果没有循环,它将捕获负值,但不会打印错误并以构建失败结束。任何帮助将不胜感激!


30秒到达战场
浏览 52回答 3
3回答

料青山看我应如是

既然你已经表现出了努力,但你似乎也是Java的新手,我正在尝试纠正你的代码。通常,不建议用于这种低水平的校正。我们需要在捕获它之前抛出一个异常 - 这就是你周围的情况 - 。NegativeValueException我假设变量 &类型 - , &是声明和定义的。NegativeValueExceptioninvalidInputkeyboard我已经提供了示例代码,对您提出的代码进行了最小的更改,尽管您提出的问题可以通过更好的代码来解决。do{&nbsp; &nbsp; &nbsp; &nbsp; invalidInput=false;&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the numerator: ");&nbsp; &nbsp; &nbsp; &nbsp; int numerator = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the denominator: ");&nbsp; &nbsp; &nbsp; &nbsp; int denominator = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if(numerator<0 && denominator <0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new NegativeValueException();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if (numerator >=0 && denominator >= 0){&nbsp; &nbsp; &nbsp; &nbsp; int quotient = numerator / denominator;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("The result of integer division is: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(quotient);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch(InputMismatchException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter only numbers.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invalidInput = true;&nbsp; &nbsp; &nbsp; &nbsp; }catch(NegativeValueException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("no negative values.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invalidInput = true;&nbsp; &nbsp; &nbsp; &nbsp; }catch(ArithmeticException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Division by zero.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invalidInput = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }while (invalidInput);&nbsp;&nbsp;

慕尼黑8549860

Scanner s = new Scanner(System.in);do {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the numerator: ");&nbsp; &nbsp; &nbsp; &nbsp; int numerator = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the denominator: ");&nbsp; &nbsp; &nbsp; &nbsp; int denominator = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; int quotient = numerator / denominator;&nbsp; &nbsp; } catch (InputMismatchException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("InputMismatchException " + e);&nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; } catch (ArithmeticException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("ArithmeticException " + e);&nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("NegativeValueException " + e);&nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; }} while (true);

HUX布斯

实际上,问题是你在外面做数学运算,比如说如果它抛出一个程序通过这些检查:try-blockexceptionif (numerator >=0 && denominator >= 0) //&nbsp; false as there was exceptionelse if(numerator<0 && denominator <0)&nbsp; // false as there was exceptionelse&nbsp; &nbsp; invalidInput = true; //gets executed执行每个异常并因此中断 .将数学移入内部invalidInput = truedo-while looptry-block试试这个代码:do{&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the numerator: ");&nbsp; &nbsp; &nbsp; &nbsp; numerator = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter a positive value for the denominator: ");&nbsp; &nbsp; &nbsp; &nbsp; denominator = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (numerator >=0 && denominator >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quotient = numerator / denominator;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(numerator<0 && denominator <0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NegativeValueException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invalidInput = true;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("The result of integer division is: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(quotient);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; catch(InputMismatchException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter only numbers.");&nbsp; &nbsp; }&nbsp; &nbsp; catch(NegativeValueException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("no negative values.");&nbsp; &nbsp; }&nbsp; &nbsp; catch(ArithmeticException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Division by zero.");&nbsp; &nbsp; }}while (invalidInput);&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java