我正在尝试做一个简单的基于文本的冒险作为初学者java的练习,但是遇到了一个问题,经过长时间的搜索,我决定只问这个问题。
我想知道如何在回答IF语句后重新触发此循环。用户输入:“帮助”,显示帮助选项卡,用户可以输入另一个命令。
我不知道为什么它不起作用,所以任何帮助将不胜感激。
boolean loopone = false;
do {
System.out.println(name + " is standing in a forest, covered in slime goo.");
String cmdone = in.next();
if (cmdone.equals("Help")) {
System.out.println("---------Help---------");
System.out.println("Type: [Help] to view help");
System.out.println("Type: [Turn] to turn, syntax: [Turn (direction)] left, right and backward are the possible directions");
System.out.println("Type: [Enter] to go through an entrance");
System.out.println("Type: [Slay] to kill stuff");
System.out.println("Type: [Take] to take stuff");
System.out.println("Typing: [/rocket smallbrains] has no effect here");
return;
}
else if (cmdone.equals("Enter")){
System.out.println("Enter what?");
String conone = in.next();
if (conone.equals("Forest") || conone.equals("The forest")){
System.out.println("You're already in the forest, dummy!");
}
else{
System.out.println("I don't know where that is");
}
}
else if (cmdone.equals("Turn right")){
System.out.println("You turn right");
}
continue;
}while (loopone = false);
最佳答案
一个问题是您需要这样做:}while (loopone = false);
这个:}while (loopone == false);
否则,您只需要确保在每个loopone
语句中将if
设置为false。要退出时,将loopone
设置为true(loopone = true
)。