//Code up
if (userinput.contains(help)) {
//Go on with the game
}
else {
System.out.println("Im sorry , couldnt understand that"); //here is where i want to go back up and
repeat the command
}
我几乎尝试了初学者会知道的所有内容,但什么也没做,在我的情况下,do while循环不起作用(也许你可以找到一种方法),如果我让if这样,如果你得到错误的答案,游戏就会关闭(有些东西出来了)上下文),一些帮助会很棒!谢谢:D
最佳答案
您可以使用“递归”函数(调用自身的函数)。
所以在这种情况下,你可以这样做:
public void getAndParseInput(){
String userInput = getUserInput() // Use whatever you're using to get input
if(userInput.contains(help)){
// If the user input contains whatever the help is (note: if you're looking for the specific word help, it needs to be in speech marks - "help").
continueWithGame...
}else{
System.out.println("Im sorry , couldnt understand that");
this.getAndParseInput();
}
}
关于java - 我正在尝试制作一个看起来像 PC 终端的基于文本的游戏,我找不到一种方法来告诉用户他们是否使用了错误的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60885044/