简体   繁体   中英

Multiple Exception Handling for multiple user inputs

I am writing a simple division program. The User enter the numerator and the denominator. The numerator and denominator is to throw an exception if the user does not enter an integer. In addition, the denominator should throw an exception if the user enter 0. The error should tell the user each time what they did wrong and continue to loop until the correct input is entered. Then it should exit when the values are correct.

Why when the user put in incorrect entries for the denominator it request the numerator again?

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");
}

}

您捕获了异常,但continueLoop的值从未更新

You need a nested while loop for that. That's an answer for your italicized question. It ask for the numerator again because your do while loop starts from entering the numerator.

If you want the program to stop when an exception is thrown you can do this:

     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");

But right now you catch is inside the while loop and your program prints the messages from the exceptions and continue again. If you want to continue this way you can enter a validation after the exception if the person wants to continue to run your program or something like that which you can connect to your continueLoop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM