我正在尝试制作一个简单的基于文本的冒险作为初学者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);
否则,您只需确保在每个 if
语句中将 loopone
设置为 false。当您想退出时,请将 loopone
设置为 true (loopone = true
)。
关于IF 语句完成后 Java 重新触发循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655834/