为什么我不能在java中使用2个switch语句

时间:2013-11-21 16:32:30

标签: java

所以我正在尝试编写一个程序来订购商品。

我必须使用switch语句来执行此操作,但我遇到的问题是如果我在第一次调用它之后尝试使用第二个开关时使用switch语句

甚至在切换语句完成后要求其他输入也似乎没有正确调用它们。

我试过的代码就是这个

public static void main(String[] args) {

    int menu = 0;
    int seldelivery = 0;
    double delivery = 0;    
    double selection = 0;
    String name;
    String student;
    String mobile;

    Scanner reader = new Scanner (System.in);       
    System.out.println("Please select\n1:Coffee\n2:Tea\n3:Quit");
    menu = reader.nextInt();

    switch (menu){
    case 1:
        System.out.println("1 Cappucino €2.00\n2 Latte €2.00\n3 Espresso €1.50\n4 Americano €1.70");
        menu = reader.nextInt();
        if (menu == 1) {
            selection = 2.00;
            System.out.println("You have selected Cappucino");
        } else if (menu == 2) {
            selection = 2.00;
            System.out.println("You have selected Latte");
        } else if (menu == 3) {
            selection = 1.50;
            System.out.println("You have selected Espresso");
        } else if (menu == 4) {
            selection = 1.70;
            System.out.println("You have selected Americano");
        } else {
            System.out.println("inavlid selection please try again");
        }
        break;

    case 2:
        System.out.println("1.Bewley’s Breakfast Tea (Pot for €1.80)\n2.Peppermint €1.50\n3 Camomile  €1.50");
        menu = reader.nextInt();
        if (menu == 1) {
            selection = 1.80;
            System.out.println("You have selected Bewley’s Breakfast Tea");
        } else if (menu == 2) {
            selection = 1.50;
            System.out.println("You have selected Peppermint");
        } else if (menu == 3) {
            selection = 1.50;
            System.out.println("Camomile");
        } else {
            System.out.println("Invalid selection");
        }
        break;
    case 3: //exit
        System.exit(0);
    default:
        System.out.println("Invalid selection");
        break;
    }

    System.out.println("Please choose one of the following");
    System.out.println("1 Delivery €1.00 extra");
    System.out.println("2 Pickup at canteen");

    delivery = reader.nextInt();

    switch (seldelivery) {
      case 4:
          System.out.println("1 Delivery will add €1.00");
          break;
      case 5:
          System.out.println("2 Please head to the canteen to collect your order");
          break;
      default:
          System.out.println("Invalid selection");
          break;
    } 

    System.out.println("Please enter your full name including your middle name");
    name = reader.nextLine();

    System.out.println("Please enter your student number");
    student = reader.nextLine();

    System.out.println("Please enter your phone number");
    mobile = reader.nextLine();

    }
}

我尝试编写这样的代码:

public static void main(String[] args) {

        int menu = 0;
        int menu1;
        double delivery = 0;
        double selection = 0;
        String name;
        String student;
        String mobile;

        Scanner reader = new Scanner(System.in);
        System.out.println("Please select\n1:Coffee\n2:Tea\n3:Quit");
        menu = reader.nextInt();

        switch (menu) {
            case 1:
                System.out.println("1 Cappucino €2.00\n2 Latte €2.00\n3 Espresso €1.50\n4 Americano €1.70");
                menu = reader.nextInt();
                if (menu == 1) {
                    selection = 2.00;
                    System.out.println("You have selected Cappucino");
                } else if (menu == 2) {
                    selection = 2.00;
                    System.out.println("You have selected Latte");
                } else if (menu == 3) {
                    selection = 1.50;
                    System.out.println("You have selected Espresso");
                } else if (menu == 4) {
                    selection = 1.70;
                    System.out.println("You have selected Americano");
                } else {
                    System.out.println("inavlid selection please try again");
                    menu = reader.nextInt();
                }
                System.out.println("Please choose one of the following\n1 Delivery €1.00 extra\n2 Pickup at canteen");
                menu1 = reader.nextInt();
                switch (menu1) {
                    case 1:
                        System.out.println("1 Delivery will add €1.00");
                        if (menu1 == 1) {
                            delivery = 1.00;
                            System.out.println("You have selected to have your order delivered");
                        }
                        break;
                    case 2:
                        System.out.println("2 Please head to the canteen to collect your order");
                        if (menu1 == 2) {
                            delivery = 0.00;
                            System.out.println("Please head to the canteen to collect your order");
                        }
                        break;
                    default:
                        System.out.println("Invalid selection");
                        break;
                }
                break;

            case 2:
                System.out.println("1.Bewley’s Breakfast Tea (Pot for €1.80)\n2.Peppermint €1.50\n3 Camomile  €1.50");
                menu = reader.nextInt();
                if (menu == 1) {
                    selection = 1.80;
                    System.out.println("You have selected Bewley’s Breakfast Tea");
                } else if (menu == 2) {
                    selection = 1.50;
                    System.out.println("You have selected Peppermint");
                } else if (menu == 3) {
                    System.out.println("Camomile");
                }
                break;
            case 3: //exit
                System.exit(0);
            default:
                System.out.println("Invalid selection");
                break;
        }
    }

这会阻止整个问题,交换机调用时应该不会,但如果我尝试获取我需要的字符串,则会再次打印它们。

我能看到这样做的唯一方法是询问交换机内所需的所有字符串并进行所有验证。

这是正确的做法还是我在这里遗漏了什么?

感谢任何帮助,谢谢:)

1 个答案:

答案 0 :(得分:2)

你误解了java语法是如何工作的。

在第二个示例中,您打开菜单:

switch (menu) {
  case 1:
  //if you have reached this, then menu is 1 and there is no further need to if on it, because it is 1, nothing else. It is exactly one.

}