繁体   English   中英

如何循环我的猜谜游戏?

[英]How do I loop my guessing game?

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

}

这个怎么样。 使用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);
}

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

查看有关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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM