Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我不能在java中使用2个开关状态_Java - Fatal编程技术网

为什么我不能在java中使用2个开关状态

为什么我不能在java中使用2个开关状态,java,Java,所以我正试图写一个程序来订购商品 我必须使用switch语句来实现这一点,但我遇到的问题是,如果我在第一次调用第二个switch之后尝试使用第二个switch语句,那么我就使用switch语句 即使在switch语句应该完成后请求其他输入,似乎也不能正确调用它们 我试过的代码是这样的 public static void main(String[] args) { int menu = 0; int seldelivery = 0; double delivery =

所以我正试图写一个程序来订购商品

我必须使用switch语句来实现这一点,但我遇到的问题是,如果我在第一次调用第二个switch之后尝试使用第二个switch语句,那么我就使用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;
        }
    }
这就解决了交换机在不应该调用时调用的整个问题,但如果我尝试获取所需字符串,它会再次快速打印它们

我能看到的唯一方法是请求交换机中所需的所有字符串,并执行所有验证

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


感谢您的帮助:)

您误解了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.

}

我不建议在开关本身中覆盖正在切换的变量(菜单)。只是潜在问题的不必要来源。我也读了一遍交换机是如何工作的,在如何使用它方面有一些突出的问题(第二个交换机中的情况,selDelivery从未设置)