gpt4 book ai didi

java - Switch 语句在 Java 中不适用于枚举

转载 作者:行者123 更新时间:2023-12-03 01:43:30 25 4
gpt4 key购买 nike

我像这样创建了一个枚举

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 中不适用于枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29405874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com