我是Java的新手,我不断得到案例表达式必须是常量表达式错误,有人可以帮忙吗?这是我的代码:
import java.util.Scanner;
public class IDlookup {
public void IDlookup(){
String Stone = "ID - 1";
String Granite = "ID - 1:1";
//System.out.println("Hai"); //TESTER
System.out.println("Please enter the block/item name here");
Scanner IDselectO = new Scanner(System.in);
String IDselect;
IDselect = IDselectO.next();
switch(IDselect){
case Stone:
System.out.println(Stone);
break;
case Granite:
System.out.println(Granite);
}
}
}
答案 0 :(得分:1)
错误非常明确,将Stone
和Granite
声明为常量
public class IDlookup {
private final static String STONE = "ID - 1";
private final static String GRANITE = "ID - 1:1";
...
}
答案 1 :(得分:0)
让您的String
stone
和granite
final
解决您的问题。
您也可能想要查找:
switch
statements work