需要常量字符串表达式错误

问题描述 投票:-5回答:1
class Restaurant { 
    static float Price;
    static int quantity;
    static float amount;

    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);

        String order;

        switch(order) {
            case Starter:
                quantity = sc.nextInt();
                System.out.println("Do you want to add more item.");
                Price = 2500;
                amount = Price * quantity;
                System.out.println(amount);
                break;
            case Main_Meal:
                quantity = sc.nextInt();
                System.out.println("Do you want to add more item ?");
                Price = 2000;
                amount = Price * quantity;
                System.out.println(amount);
            default:
                System.out.println("No order");
        }
    }
}
java
1个回答
3
投票

case Starter:不是有效的switch语句表达式。应引用交换机“case”中的字符串文字:

switch (order) {
  case "Starter":

这在The switch Statement docs中有描述。

© www.soinside.com 2019 - 2024. All rights reserved.