0

我是 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);
}

}

谢谢你们的帮助:)

4

3 回答 3

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
  }

}
于 2013-11-10T22:02:23.547 回答
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);
}
于 2013-11-10T22:07:46.250 回答
0

你也可以从一个无限循环开始,如果找到了魔法词就中断。

请参阅文档以了解breakwhile 循环

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;
            }
        }
    }
}
于 2013-11-10T23:26:14.783 回答