java - 如何循环猜谜游戏?

标签 java string loops restart levels

我是 Java 和这个网站的新手,并且制作了一个简单的猜谜游戏。

游戏的目的是尝试猜测魔法词。

我想知道如何循环它,这样如果你做错了问题,你可以再尝试一次,如果你做对了,你可以进入第 2 级。

任何帮助将不胜感激

package textpac;
import javax.swing.JOptionPane;
public class textclass {

public static void main(String[] args) {

    boolean rightanswer = false;
    String inputText = JOptionPane.showInputDialog("What is the magic word?");
    String outputText = null;
    if (inputText.equalsIgnoreCase("themagicword")){
        outputText = "Well done!";
        rightanswer = true;
    } 
    if (!(inputText.equalsIgnoreCase("themagicword"))){
        outputText = "Wrong!";
    }
    JOptionPane.showMessageDialog(null, outputText);
}

}

谢谢大家的帮助:)

最佳答案

这就是你想做的

import javax.swing.JOptionPane;

public class textclass {

public static void main(String[] args) {

    boolean rightanswer = false;
    while (!rightanswer) {
        String inputText = JOptionPane
                .showInputDialog("What is the magic word?");
        String outputText = null;
        if (inputText.equalsIgnoreCase("themagicword")) {
            outputText = "Well done!";
            rightanswer = true;
        }
        if (!(inputText.equalsIgnoreCase("themagicword"))) {
            outputText = "Wrong!";
        }

        JOptionPane.showMessageDialog(null, outputText);
    } //end of new while bit
  }

}

关于java - 如何循环猜谜游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895610/

相关文章:

c - K&R 第一版,练习 1.18(for 语句中的测试很笨拙)

java - 如何在Arraylist java中通过ID号搜索并显示对象?

java - 启动我的 Spring Boot 应用程序时未找到 byte-buddy

java - 如何检查 JSP 是否连接到数据库?

Java 图形绘图未显示在绘图面板上

algorithm - 标记长字符串中的有效单词

java - 检查 int 是否位于数组中的两个项目之间

javascript - 使用插值标记分割字符串

java - 将java中的字符串拆分为固定长度的 block

javascript - d3 每次迭代等待 n 秒