eclipse switch语句eclipse报错:case表达式必须是常量表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35121169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch statement eclipse error: case expressions must be constant expressions
提问by Skippy
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." 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).
我写了一个程序,它会根据我将输入的颜色输出一朵花。在 switch 语句中,我一直看到一个错误,指出“case 表达式必须是常量表达式”。我看不出我哪里做错了。我也有打印出花的复数时态的问题(如果用户输入 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 + ".");
}
}
}
回答by DominicEU
Mark the variables red
, purple
, etc, as final.
将变量red
、purple
等标记为 final。
final String red = "red";
final String blue = "blue";
final String yellow = "yellow";
回答by ronIDX
Use string literals for the color name instead of using variables.
使用字符串文字作为颜色名称而不是使用变量。
switch(color) {
case "red":
...
case "blue":
...
... //other cases also with ""
}
回答by HopefullyHelpful
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. 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).
switch 中的任何语句都必须是编译时常量,这意味着它必须是一个常量,它是文字或由文字赋值。文字是直接写入代码的所有值。常量是在赋值后不能改变的值,这意味着任何最终值都是一个常量。强烈建议将静态修饰符用于数学常数(并减少实例使用的空间)。
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.
将不起作用,因为该方法无法在编译时执行。
回答by HopefullyHelpful
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 + ".");
}
}
回答by antroid
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;
}
}