Java Switch案例

时间:2015-10-21 15:44:17

标签: java switch-statement

当我进入子菜单并完成例如案例1,案例完成后,它会自动跳转到主菜单的案例2,任何人都可以告诉我如何停止这个,因为我想要它再次显示主菜单

switch (choice) {
    case 1:    //finish add passenger
        System.out.println(" 1 - Add passenger");
        System.out.println(mySubMenu.displayMenu());
        int subChoice = mySubMenu.getChoice();
        switch (subChoice) {
            case 1:
                try {
                    System.out.println("Please enter the passengers forename");
                    String aforename = strKeyB.next();
                    System.out.println("Please enter the passengers surname");
                    String surname = strKeyB.next();

                    System.out.println("Please enter the passengers nationality");
                    String nationality = strKeyB.next();

                    System.out.println("Please enter the passenger age");
                    int age = intKeyB.nextInt();
                    System.out.println(pass1.pasengerAdd(aforename, surname, nationality, age));
                    break;
                } catch (Exception ex) {
                    System.out.println(ex.toString());

                }

            case 2:
                try {
                    System.out.println("Please enter the passengers forename");
                    String onforename = strKeyB.next();
                    System.out.println("Please enter the passengers surname");
                    String onSurname = strKeyB.next();
                    pass1.pasengerAdd(onforename, onSurname);

                } catch (Exception ex) {
                    System.out.println(ex.toString());

                }
                break;
            case 3:
                try {
                    System.out.println("Please enter the passengers forename");
                    String nnforename = strKeyB.next();
                    System.out.println("Please enter the passengers surname");
                    String nnSurname = strKeyB.next();
                    System.out.println("Please enter the passengers age");
                    int intAge = intKeyB.nextInt();
                    pass1.pasengerAdd(nnforename, nnSurname, intAge);


                } catch (Exception ex) {
                    System.out.println(ex.toString());

                }//end catch 
                break;
            case 4:
                try {
                    System.out.println("Please enter the passengers forename");
                    String naforename = strKeyB.next();
                    System.out.println("Please enter the passengers surname");
                    String naSurname = strKeyB.next();
                    System.out.println("Please enter the passengers nationality");
                    String naNat = strKeyB.next();
                    pass1.pasengerAdd(naforename, naSurname, naNat);

                } catch (Exception ex) {
                    System.out.println(ex.toString());

                }
                break;
            case 5:
                System.out.println(myMenu.displayMenu());
                break;
        }//end sub menu switch


    case 2:
        try {
            System.out.println(" 2 - Delete Passenger");
            System.out.println("Please enter the passenger forename ");
            String dForename = strKeyB.next();
            System.out.println("Please enter the passenger surname ");
            String dSurname = strKeyB.next();
            System.out.println(pass1.passengerDelete(dForename, dSurname));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        break;
}

1 个答案:

答案 0 :(得分:1)

在每个break块的末尾使用case

问题在于你有两个case 1:

  • 案例1:switch(choice)
  • 案例1:switch(subchoice)

你遗漏了两件事:

  • break中的switch(choice)语句,因此case 2:的代码将被执行。
  • case 1:switch(subchoice)的catch分支。

看一下这个简单的案例:

switch(choice) {
case 1:
    // code1
    break;
case 2: 
    // code2
    break;
}

此处案例代码将单独执行,

否则在此代码中:

switch(choice) {
case 1:
    // code1
case 2: 
    // code2
}

如果未使用break,代码会不断执行,因此会执行choice==1 code1 AND code2。但是,如果choice==2只执行code2当2个或更多选项共享某些代码时,真的有用。

注意:这也是有效的:

switch(choice) {
case 1:
case 2: 
    // code1
    // code2
    break;
}

在此声明中,Case 1Case 2将执行相同的代码。

SOLUTION:

了解这一点,您的代码必须有2个修复程序:

  1. break
  2. 代码之前的switch(choice)中插入case 2:语句
  3. breakcase 1:的{​​{1}}语句移至switch(subchoice)的代码
  4. 之前的末尾

    case 2: