代码之家  ›  专栏  ›  技术社区  ›  icekomo

在Do-While循环中使用try/Catch

  •  2
  • icekomo  · 技术社区  · 4 年前

    private boolean madeChoice = false;
    
    public void whatToDo(){
        System.out.println("");
        System.out.println("");
        System.out.println("=====================================");
        System.out.println("Welcome");
        System.out.println("=====================================\n");
        System.out.println("What would you like to do today? Your choices are: \n");
        choices();
    
        do{
            try{
                System.out.println("");
                System.out.println("Please enter your choice");
                int numberEntered = keyboard.nextInt();
    
                if(numberEntered > 3 || numberEntered < 1){
                    System.out.println("-----------------------------------------------------------");
                    System.out.println("That is not a choice. Please choose from the following: \n");
                    choices();
                    numberEntered = keyboard.nextInt();
                   
                }else{
                    System.out.println("That is a good CHOICE");
                    madeChoice = true;
                }
              
            }catch (InputMismatchException e){
                System.out.println("-----------------------------------------------------------");
                System.out.println("This is not a number. Please choose from the following: \n");
                choices();
                
            }
    
        }while(!madeChoice);
    
    }
    
    private void choices(){
        System.out.println("1. Add a new set into your account");
        System.out.println("2. See all the sets in your account");
        System.out.println("3. Exit the Account Manger.");
    }
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   zysaaa Priscila Lopes Svolinski    4 年前

    添加 keyboard.nextLine() continue ,当发生异常时,您需要“吃掉”输入:

               catch (InputMismatchException e){
                    System.out.println("-----------------------------------------------------------");
                    System.out.println("This is not a number. Please choose from the following: \n");
                    choices();
                    keyboard.nextLine();
                    continue;
                    // numberEntered = keyboard.nextInt();
                }
    

    编辑 ,我更改了您的一些代码:

    public void whatToDo(){
            System.out.println("");
            System.out.println("");
            System.out.println("=====================================");
            System.out.println("Welcome");
            System.out.println("=====================================\n");
            System.out.println("What would you like to do today? Your choices are: \n");
            Scanner keyboard = new Scanner(System.in);
            do{
                try{
                    choices();
                    System.out.println("");
                    System.out.println("Please enter your choice");
                    int numberEntered = keyboard.nextInt();
    
                    if (numberEntered >= 1 && numberEntered <= 3) {
                        System.out.println("That is a good CHOICE");
                        madeChoice = true;
                    } else {
                        System.out.println("-----------------------------------------------------------");
                        System.out.println("That is not a choice. Please choose from the following: \n");
                    }
                }catch (InputMismatchException e){
                    System.out.println("-----------------------------------------------------------");
                    System.out.println("This is not a number. Please choose from the following: \n");
                    keyboard.next();
                    // numberEntered = keyboard.nextInt();
                }
    
            }while(!madeChoice);
    
        }
    
        2
  •  0
  •   Akash Patel    4 年前

    也许您可以将代码更新为这样的内容,这对我来说似乎很有用:

    private boolean madeChoice = false;
    private static boolean isMissMatchException = false ;
    private static boolean isInvalidInput = false ;
    
    public void whatToDo(){
        if(Test.isMissMatchException) 
        {
             System.out.println("-----------------------------------------------------------");
             System.out.println("This is not a number. Please choose from the following: \n");
             Test.isMissMatchException = false ;
        } else if (Test.isInvalidInput) 
        {
            System.out.println("-----------------------------------------------------------");
            System.out.println("That is not a choice. Please choose from the following: \n");
            Test.isInvalidInput = false ;
        } else {            
            System.out.println("");
            System.out.println("");
            System.out.println("=====================================");
            System.out.println("Welcome");
            System.out.println("=====================================\n");
            System.out.println("What would you like to do today? Your choices are: \n");
        }
        choices();
        
        do{
            try{
                System.out.println("");
                System.out.println("Please enter your choice");
                
                @SuppressWarnings("resource")
                int numberEntered = new Scanner(System.in).nextInt();
    
                if(numberEntered > 3 || numberEntered < 1){
                    Test.isInvalidInput = true ;
                    this.whatToDo();
                   
                }else{
                    System.out.println("That is a good CHOICE");
                    madeChoice = true;
                }
              
            }catch (InputMismatchException e){
                Test.isMissMatchException = true ;
                this.whatToDo();
            }
    
        }while(madeChoice);
    
    }
    
    private void choices(){
        System.out.println("1. Add a new set into your account");
        System.out.println("2. See all the sets in your account");
        System.out.println("3. Exit the Account Manger.");
    }
    
    推荐文章