[英]Switch statement not working in Java with enum
I created an enum like so 我创建了一个这样的枚举
public enum Direction {
NORTH, SOUTH, WEST, EAST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
}
then I try to use it in a switch statement 然后我尝试在switch语句中使用它
Direction direction = Direction.NORTH;
switch(direction){
NORTH:
System.out.println("Syntax error on token {, case expected after this token");
break;
}
I am getting the error I put in the println... 我收到了我在println中输入的错误...
While not directly answering the question, I would suggest adding a method to the enum ( Java Enum Methods ) and calling the method instead. 虽然没有直接回答这个问题,但我建议在枚举中添加一个方法( Java Enum Methods )并调用该方法。 This would make it cleaner if and when we add a new enum type. 如果我们添加新的枚举类型,这将使它更清晰。 We don't have to make modifications to the switch case, just add the implementation for the newly added enum type. 我们不必对switch case进行修改,只需添加新添加的枚举类型的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.