我像这样创建了一个枚举

public enum Direction {
    NORTH, SOUTH, WEST, EAST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
}


然后我尝试在switch语句中使用它

 Direction direction = Direction.NORTH;
    switch(direction){
    NORTH:
        System.out.println("Syntax error on token {, case expected after this token");
        break;
    }


我收到我在println中输入的错误...

最佳答案

您错过了case关键字。

switch(direction){
case NORTH:
    System.out.println("Syntax error on token {, case expected after this token");
    break;
}


Demo

关于java - switch语句无法在Java中与enum一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29405874/

10-15 23:27