NORTH("north"),
SOUTH("south"),
WEST("west"),
EAST("east");
String direction;
private RoomDirection(String direction){
this.direction=direction;
}
public String getDirection(){
return this.direction;
}
public boolean isSecondCommand(String aString){
aString=aString.toUpperCase();
for(RoomDirection dir :RoomDirection.values()){
if(aString.equalsIgnoreCase(dir.direction))
return true;
}
return false;
}
boolean static c= RoomDirection.isSecondCommand(word2);
if(commands.isCommand(word1)&&c.isSecondCommand(word2))
return new Command(word1,word2);`
你好我们刚接触java和iam写一个zork游戏作为一名大学生...我想把方向放在东西南方向的enum文件中,然后将它们带到主要的....我搜索了一个这两天很多......我想出了这样的事情.......
调用方法的错误是我的主类
中的枚举类RoomDirection的第二个命令答案 0 :(得分:0)
我修改了你的代码,如下所示。我认为这就是你想要做的事情。
enum RoomDirection {
NORTH, SOUTH, WEST, EAST
}
public class TestEnum {
String direction;
RoomDirection roomDirection;
private void RoomDirection(String direction) {
this.direction = direction;
}
public String getDirection() {
return this.direction;
}
public boolean isSecondCommand(String d) {
for (RoomDirection dir : RoomDirection.values()) {
if(d.equalsIgnoreCase(dir.name())) {
return true;
}
}
return false;
}
public static void main(String[] args) {
TestEnum te = new TestEnum();
System.out.println(te.isSecondCommand("abcd")); // false, because RoomDirection doesn't contain a value named abcd
System.out.println(te.isSecondCommand("north")); // true
System.out.println(te.isSecondCommand("NORTH")); // true
}
}
isSecondCommand()是一个函数,它允许您输入一个String并测试枚举RoomDirection是否包含此String。