【问题标题】:Switch statement eclipse error: case expressions must be constant expressionsSwitch语句eclipse错误:case表达式必须是常量表达式
【发布时间】:2016-02-01 00:11:31
【问题描述】:

我写了一个程序,它会根据我输入的颜色输出一朵花。在 switch 语句中,我一直看到一个错误,指出“case 表达式必须是常量表达式”。我看不出我在哪里做错了。我还遇到了打印出花朵复数时态的问题(如果用户输入 2 或更高)。

代码如下:

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

}

【问题讨论】:

  • 不能在 case 语句中使用变量。它们必须是常量

标签: java eclipse switch-statement


【解决方案1】:

将变量redpurple等标记为final。

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

【讨论】:

    【解决方案2】:

    对颜色名称使用字符串文字而不是使用变量。

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

    【讨论】:

      【解决方案3】:

      switch 中的任何语句都必须是一个编译时常量,这意味着它必须是一个常量,它是一个字面量或由字面量赋值。文字是直接写入代码的所有值。常量是赋值后不能改变的值,这意味着任何最终值都是常量。强烈建议将静态修饰符用于数学常量(并减少实例使用的空间)。

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

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

      两者都可以。

      另一方面,返回字符串的函数将不起作用:

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

      不起作用,因为该方法无法在编译时执行。

      【讨论】:

        【解决方案4】:

        char 的标准值显示为矩形框,如果您想将空字符作为标准值,请使用字符串。还可以将 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 + ".");
            }
        }
        

        【讨论】:

          【解决方案5】:

          代码应该是这样的: 扫描仪输入 = 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; 
              } 
          
          } 
          

          【讨论】:

          • 最后两行的风格真的很糟糕,复制代码总是会导致错误,因为如果你更改了一行,很容易忘记更改另一行,而且手动更改每一行都很麻烦。我认为 OP 的原始打印方式更好。
          • 真的。这就是为什么在那里使用休息。谢谢@HopefullyHelpful
          猜你喜欢
          • 1970-01-01
          • 2011-06-12
          • 1970-01-01
          • 1970-01-01
          • 2011-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多