【问题标题】:Multiple Exception Handling for multiple user inputs多用户输入的多异常处理
【发布时间】:2024-04-20 22:30:02
【问题描述】:

我正在编写一个简单的除法程序。用户输入分子和分母。分子和分母是如果用户不输入整数则抛出异常。此外,如果用户输入 0,分母应该抛出异常。错误应该告诉用户每次他们做错了什么,并继续循环,直到输入正确的输入。然后它应该在值正确时退出。

为什么当用户输入不正确的分母条目时,它会再次请求分子?

import java.util.InputMismatchException;
import java.util.Scanner;

public static void main(String[] args) 
{
    // TODO Auto-generated method stub  
    System.out.printf("Welcome to Division \n");

    Scanner div = new Scanner(System.in);
    boolean continueLoop = true;
do
{
    try 
    {
    System.out.println("\nEnter a numerator:\n");
    int num1 = div.nextInt();
    System.out.println("Enter a denominator:");
    int num2 = div.nextInt();

    int result = quotient(num1, num2);
    System.out.printf("The Answer is: %d / %d = %d%n",num1,num2,result);
    }
    catch(InputMismatchException inputMismatchException)
    {
        System.err.printf("\n Exception \n",inputMismatchException );
        div.nextLine();
        System.out.printf("You must enter integers");

    }
    catch(ArithmeticException arithmeticException)
    {
        System.err.printf("\n Exception \n",arithmeticException);
        System.out.printf(" Zero is an invalid entry");
    }
} while (continueLoop);

    System.out.printf("\n GOODBYE \n");
}

}

【问题讨论】:

  • 您只有一个循环,因此每次运行时都会执行所有步骤。在调试器下逐步运行您的代码并亲自查看。

标签: java exception-handling user-input java-7


【解决方案1】:

您捕获了异常,但 continueLoop 的值永远不会更新

【讨论】:

    【解决方案2】:

    为此,您需要一个嵌套的 while 循环。这是您的斜体问题的答案。它再次询问分子,因为您的 do while 循环从输入分子开始。

    【讨论】:

    • 我尝试添加一个 if 语句,但它不会编译。
    • 你必须启动 2 个 do while 循环。我在 C++ 中执行此操作。首先,开始做while并尝试输入分子。之后是一个捕获。然后在那个do里面,放另一个do来输入分母。然后又抓到了。现在你已经写出了完美的 2 while 语句。第一个 while 用于分母输入,如果您有正确的 while 语句,循环将返回到该点。第二个 while 将再次从顶部开始循环。现在——如果你试图输入错误的分子,它必须再次跳回顶部。
    【解决方案3】:

    如果您希望程序在抛出异常时停止,您可以这样做:

         try {
                do {
    
                    System.out.println("\nEnter a numerator:\n");
                    int num1 = div.nextInt();
                    System.out.println("Enter a denominator:");
                    int num2 = div.nextInt();
    
                    int result = quotient(num1, num2);
                    System.out.printf("The Answer is: %d / %d = %d%n", num1, num2, result);
                } while (continueLoop);
            } catch (ArithmeticException arithmeticException) {
                System.err.printf("\n Exception \n", arithmeticException);
                System.out.printf(" Zero is an invalid entry");
            } catch (InputMissMatchException inputMismatchException) {
                System.err.printf("\n Exception \n", inputMismatchException);
                div.nextLine();
                System.out.printf("You must enter integers");
    
            }
            System.out.printf("\n GOODBYE \n");
    

    但现在你的 catch 是在 while 循环中,你的程序会打印来自异常的消息并再次继续。如果您想以这种方式继续,您可以在异常之后输入验证,如果该人想继续运行您的程序或类似的东西,您可以连接到您的 continueLoop

    【讨论】:

    • 谢谢。但我希望它建议用户输入一个整数作为分母,而不是从头开始
    • 那么您可以制作两个单独的 try catch 块,并且带有分母的块应该处于 do-while 循环中,直到成功进入。您可以为有效输入创建一个布尔值,并在 catch 块中向用户显示您的消息 make valid = false。当它进入 while 条件时,它会看到布尔值为 false 并再次返回分母消息。