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

时间:2019-02-22 20:01:39

标签: java

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");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

case Starter:不是有效的switch语句表达式。开关“ case”中的字符串文字应加引号:

switch (order) {
  case "Starter":

这在The switch Statement docs中有描述。