我写了一个程序,根据我输入的颜色输出一朵花。在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 + ".");
}
}
答案 0 :(得分:2)
将变量red
,purple
等标记为最终。
final String red = "red";
final String blue = "blue";
final String yellow = "yellow";
答案 1 :(得分:2)
开关中的任何语句必须是编译时常量,这意味着它必须是文字的常量或文字的分配。文字都是直接写入代码的值。常量是在分配之后无法更改的值,这意味着任何最终值都是常量。静态修改器强烈建议使用数学常量(并减少实例使用的空间)。
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");
不起作用,因为该方法无法在编译时执行。
答案 2 :(得分:1)
使用字符串文字作为颜色名称而不是使用变量。
switch(color) {
case "red":
...
case "blue":
...
... //other cases also with ""
}
答案 3 :(得分:0)
char的标准值显示为rectancle框,如果要将空字符作为标准值,请使用字符串。也可以使用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 + ".");
}
}
答案 4 :(得分:-1)
这是代码应该是这样的: 扫描仪输入=新扫描仪(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;
}
}