java - 我正在尝试制作一个看起来像 PC 终端的基于文本的游戏,我找不到一种方法来告诉用户他们是否使用了错误的句子

标签 java

//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/

相关文章:

java - 如何在 Maven 中为 war 文件生成校验和

java - 自定义错误报告 : how to log down function arguments (and possibly locals) in an exception handler?

java - 升级到 Android 3.1.2 后出现 AAPT2 错误

java - 如何从实现相同接口(interface)的多个 bundle 中使用正确的 bundle ?

java - 每 20 毫秒将值从 Web 服务器推送到客户端 (Java MVC)

java - 如何在 thymeleaf 中转义 'And' 运算符

java - 将参数传递给 pig udf 无法使用参数实例化

java - JSwing,如何在开始另一个操作之前进行 Jtable 渲染?

java - 我需要将标志放入同步块(synchronized block)中吗?

java - 多个 JAXBContext 实例