[英]Case expressions must be constant expressions
I'm a newbie at Java, and I kept getting the case expressions must be constant expressions error, can someone please help? 我是Java的新手,我一直在获取case表达式必须为常量表达式错误的消息,有人可以帮忙吗? This is my code:
这是我的代码:
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);
}
}
}
The error is pretty clear, declare Stone
and Granite
as constants 错误很明显,将
Stone
和Granite
声明为常量
public class IDlookup {
private final static String STONE = "ID - 1";
private final static String GRANITE = "ID - 1:1";
...
}
Make your String
s stone
and granite
final
to fix your issue. 使
String
的stone
和granite
final
解决您的问题的方法。
You also probably want to look up: 您可能还想查找:
switch
statements work switch
语句如何工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.