简体   繁体   English

switch语句eclipse报错:case表达式必须是常量表达式

[英]Switch statement eclipse error: case expressions must be constant expressions

I wrote a program that would output a flower based off the color I will input.我写了一个程序,它会根据我将输入的颜色输出一朵花。 In the switch statement, I keep seeing an error stating that the "case expressions must be constant expressions."在 switch 语句中,我一直看到一个错误,指出“case 表达式必须是常量表达式”。 I don't see where I am doing it wrong.我看不出我哪里做错了。 I'm also having an issue of printing out the plural tense of the flower (if the user would input 2 or higher).我也有打印出花的复数时态的问题(如果用户输入 2 或更高)。

Here's the code:这是代码:

Scanner input = new Scanner(System.in);

    int quantity;
    String color;
    String flowerType = "";
    char extra;

    System.out.print("Please enter a color: ");
    color = input.next();
    System.out.print("Please enter the quantity: ");
    quantity = input.nextInt();

    String red = "red";
    String blue = "blue";
    String yellow = "yellow";
    String purple = "purple";
    String white = "white";

    switch(color){
    case red:
        flowerType = "rose";
        break;
    case blue:
        flowerType = "iris";
        break;
    case yellow:
        flowerType = "daffodil";
        break;
    case purple:
        flowerType = "sage";
        break;
    case white:
        flowerType = "dogwood";
        break;
        default:
            System.out.println("Invalid color.");
    }
    switch(quantity){
    case 1:
        break;
    default:
        extra = 's';
        break;
    }
    System.out.println("You have " + quantity + flowerType + extra + ".");
}

} }

Mark the variables red , purple , etc, as final.将变量redpurple等标记为 final。

final String red = "red";
final String blue = "blue";
final String yellow = "yellow";

Use string literals for the color name instead of using variables.使用字符串文字作为颜色名称而不是使用变量。

switch(color) {
case "red":
     ...
case "blue":
     ...
... //other cases also with ""
}

Any statement in a switch must be a compile-time constant , which means it must be a constant that is a literal or is assignet by a literal. switch 中的任何语句都必须是编译时常量,这意味着它必须是一个常量,它是文字或由文字赋值。 Literals are all values written directly into code.文字是直接写入代码的所有值。 Constants are values that can't change after beeing assigned, which means any final value is a constant.常量是在赋值后不能改变的值,这意味着任何最终值都是一个常量。 The static modifier is highly advised for mathematical constants (and to reduce space used by instances).强烈建议将静态修饰符用于数学常数(并减少实例使用的空间)。

final String red = "red";
case red:
     ... //insert code here
     break;

and

case "red":
     ... //insert code here
     break;

will both work.两者都会起作用。

On the other side functions returning a string won't work:另一方面,返回字符串的函数将不起作用:

static final String s = new String("abc");
final String s = new String("abcd");

won't work, because the method can't be executed at compiletime.将不起作用,因为该方法无法在编译时执行。

The standard value for a char is shown as a rectancle box, use a string if you want to have an empty character as standard value.字符的标准值显示为矩形框,如果您希望将空字符作为标准值,请使用字符串。 Also use if's for conditions and not switches.也使用 if 来表示条件而不是开关。

package pointless;

import java.util.Scanner;

public class SO {
    static final String red = "red";
    static final String blue = "blue";
    static final String yellow = "yellow";
    static final String purple = "purple";
    static final String white = "white";
    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);

        int quantity;
        String color;
        String flowerType = "";
        String extra = ""; //using a string is better cause it can be empty, a char can't be empty

        System.out.print("Please enter a color: ");
        color = input.next();
        System.out.print("Please enter the quantity: ");
        quantity = input.nextInt();

        switch(color){
            case red:
                flowerType = "rose";
                break;
            case blue:
                flowerType = "iris";
                break;
            case yellow:
                flowerType = "daffodil";
                break;
            case purple:
                flowerType = "sage";
                break;
            case white:
                flowerType = "dogwood";
                break;
            default:
                System.out.println("Invalid color.");
        }
        if(quantity>1) { // use a if for checking conditions like quantity>1
            extra="s";
        }
        System.out.println("You have " + quantity+ " " + flowerType + extra + ".");
    }
}

This is something the code should look like : Scanner input = new Scanner(System.in);代码应该是这样的: Scanner input = new Scanner(System.in);

    int quantity;
    String color;
    String flowerType = "";
    char extra;

    System.out.print("Please enter a color: ");
    color = input.next();
    System.out.print("Please enter the quantity: ");
    quantity = input.nextInt();

    final String  red = "red";
    final String blue = "blue";
    final String yellow = "yellow";
    final String purple = "purple";
    final String white = "white";

    switch(color){
    case red:
        flowerType = "rose";
        break; 
    case blue:
        flowerType = "iris";
        break; 
    case yellow:
        flowerType = "daffodil";
        break; 
    case purple:
        flowerType = "sage";
        break; 
    case white:
        flowerType = "dogwood";
        break; 
        default: 
            System.out.println("Invalid color.");
    } 
    switch(quantity){
    case 1: 
        System.out.println("You have " + quantity + flowerType+".");
        break; 
    default: 
        extra = 's';
        System.out.println("You have " + quantity + flowerType + extra + ".");
        break; 
    } 

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM