简体   繁体   中英

Is there a reason why a try-catch nested within a do-while loop not actually loop?

I have a simple switch-case nested onto a do while loop. in my code, so the input for the switch case is through the user. So I have tried to have an InputMismatchException caught then the loop to continue {loop back again} yet it stops everything. How do I make it continue looping?

   public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Equation Solver");          
        char ch;
        do
        {
            try{
            System.out.println("\nPlease choose\n");
            System.out.println("1. Suvat Equations");
            System.out.println("2. Energy Equations");
            System.out.println("3. The Doppler Effect Calculations");
            System.out.println("4. Gravitational Force Equation");
            System.out.println("5. Support");
            System.out.println("6. Graph");

            int choice = scan.nextInt();

            switch (choice)
            {
             case 1 : 
                System.out.println("\nPlease choose according to the variable you want to find\n");
                System.out.println("1. Initial Velocity");
                System.out.println("2. Final Velocity");
                System.out.println("3. Acceleration");
                System.out.println("4. Distance Travelled");
                System.out.println("5. Time Taken");
                System.out.println("6. Otherwise Cancel");

                try{ 
                    int choiceCase1 = scan.nextInt();

                    switch(choiceCase1){
                        case 1 :
                            System.out.println("Please ");
                            Equation1();
                            break;
                        case 2 :
                            Equation1();
                            break;
                        case 3 :
                            Equation1();
                            break;
                        case 4 :
                            Equation1();
                            break;  
                        case 5 :
                            Equation1();
                            break;
                        case 6 :
                            break; 
                        default :
                            System.out.println("Wrong Entry \n ");
                            break;    
                    }



                }catch (InputMismatchException e){
                        System.out.println("please stop");
                 }

            case 2 : 
                Equation1();                     
                break;                          
            case 3 : 
                System.out.println("Enter integer element to insert");

                break;                         
            case 4 : 
                System.out.println("Enter integer element to insert");

                break;                                          
            case 5 : 

                break;     
            case 6 : 

                break;            
            default : 
                System.out.println("Wrong Entry \n ");
                break;   
            }    
            } catch (InputMismatchException s){
                System.out.println("Caught Exception")
            } finally{

            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);    
            }
        } while (ch == 'Y'|| ch == 'y'); 

        }

I expected for the user to be able to input y then repeat the loop yet does not allow it

From: https://www.tutorialspoint.com/java/java_break_statement.htm

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

Your break statement at:

    } catch (InputMismatchException s){
        break;
    }

is ending the loop.

Edit: This try/catch statement is outside of the switch statement. So the break is not exiting the switch statement, it is exiting the loop.

It looks like when you catch the exception you break out of the Do-While loop. I think you could probably remove that break command and it would do nothing with the exception but continue with the loop. By having the break in there if you catch the exception it will terminate the loop.

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