如何循环我的猜谜游戏?

时间:2013-11-10 21:46:33

标签: 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);
}

}

感谢帮助人员:)

3 个答案:

答案 0 :(得分:1)

这就是你想要做的事吗

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
  }

}

答案 1 :(得分:1)

这个怎么样?使用do-while循环。

package textpac;

import javax.swing.JOptionPane;

public class TextClass {

public static void main(String[] args) {

    boolean rightAnswer = false;
    String inputText = null;
    String outputText = null;
    int numberOfAttempts = 0;
    do {
        numberOfAttempts++;
        if(numberOfAttempts == 1) {
            inputText = JOptionPane.showInputDialog("What is the magic word?");
        } 
        else {
            inputText = JOptionPane.showInputDialog("Try again. What is the magic word?");
        }

        if (inputText.equalsIgnoreCase("themagicword")){
            outputText = "Well done!";
            rightAnswer = true;
        } 
        else {
            outputText = "Wrong!";
            if(numberOfAttempts > 1) {
                outputText += " Game over.";
            }
        }
        JOptionPane.showMessageDialog(null, outputText);
    } while(numberOfAttempts < 2 && !rightAnswer);
}

答案 2 :(得分:0)

您也可以从无限循环开始,然后在找到魔术词时中断。

查看breakwhile loop

的文档
import javax.swing.JOptionPane;

public class textclass {

    public static void main(String[] args) {
        //infinite loop
        while (true) {
            String inputText = JOptionPane.showInputDialog("What is the magic word?");

            if (!(inputText.equalsIgnoreCase("themagicword"))) {
                JOptionPane.showMessageDialog(null, "Wrong!");
            }

            if (inputText.equalsIgnoreCase("themagicword")) {
                JOptionPane.showMessageDialog(null, "Well done!");

                //magic word found, "break" the loop
                break;
            }
        }
    }
}