【问题标题】:program terminate after using try catch block in switch statement在 switch 语句中使用 try catch 块后程序终止
【发布时间】:2016-04-28 00:18:09
【问题描述】:

以下代码在 try catch 块捕获异常后终止。它不允许我从菜单选项中进行选择。所以我的问题是我必须对此代码进行哪些更改,以便我可以循环回来,以便我可以再次获得用户输入。

    public class Main {

        public static void main(String[] args) {

            Modify modifyObj = new Modify();

            int choice = 0 ;

           Scanner input = new Scanner(System.in);

            //begin loop
            do {
                 try{

                //display menu

                System.out.println("Choose one option from following option available: ");
                System.out.println("0) Exit program. ");
                System.out.println("1) Create a Roster");
                System.out.println("2) Modify a Roster");
                System.out.println("3) Delete a Roster");



                choice = input.nextInt(); //gets user input 

                    switch (choice) {

                        case 1:
//code
                         break;


                        case 2:
    //code
                                break;

                        case 3:
                            //code
                           break;


                    }// end of switch statement
                    break;
                }//end oftry
                 catch(InputMismatchException inputMismatchException){
                    System.out.println("Enter integer  value between 0 and 7:"); 
                    continue;
                 }


            }while (choice!=0); //loop until user exit 0.

        }//end of main
    }// end of Main class

【问题讨论】:

  • 为什么switch(choice)块结束后还有break;?这不会把你踢出循环吗?
  • 那是我的代码中的错字,我刚改了仍然有同样的问题。
  • 我怀疑@Elliot 的答案解决了异常情况下的问题。无关的break 的存在防止重复循环,直到选择退出。

标签: java switch-statement try-catch


【解决方案1】:

确保choicecontinue; 之前不是0

catch(InputMismatchException inputMismatchException){
    System.out.println("Enter integer  value between 0 and 7:"); 
    choice = 1; // <-- not 0.
    continue;
}

注意默认 choice 的初始值是0

你可以使用方法

如果您将您的逻辑提取到一个(或两个)实用程序方法中来显示菜单并让用户选择它会简化事情;像

private static void showMenu() {
    System.out.println("Choose one option from following option available: ");
    System.out.println("0) Exit program. ");
    System.out.println("1) Create a Roster");
    System.out.println("2) Modify a Roster");
    System.out.println("3) Delete a Roster");
}

private static int getUserOption(Scanner input) {
    while (true) {
        showMenu();
        if (input.hasNextInt()) {
            int t = input.nextInt();
            switch(t) {
            case 0: case 1: case 2: case 3:
                return t;
            }
        } else {
            input.nextLine();
        }
    }
}

那么你的main 可以像这样调用它

public static void main(String[] args) {
    Modify modifyObj = new Modify();

    Scanner input = new Scanner(System.in);
    int choice;
    // begin loop
    do {
        choice = getUserOption(input);
        if (choice != 0) {
            System.out.printf("You chose %d.%n", choice);
        }
    } while (choice != 0); // loop until user enters 0.
}// end of main

【讨论】:

  • 切换后有break。这不是原因而不是这个吗?
  • @Gendarme 也许我愚蠢地相信 OP 关于 try catch 块捕获异常后以下代码终止
  • 当我将选项更改为 1 时,我的代码将无限运行。
  • 您的break 声明适用于switch;如果你想break 循环添加标签。
  • 或者,更好将您的逻辑提取到方法中并调用它们。见编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 2023-03-18
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多