简体   繁体   English

如何循环我的猜谜游戏?

[英]How do I loop my guessing game?

I'm new to Java and this site, and have made a simple guessing game. 我是Java和这个网站的新手,并制作了一个简单的猜谜游戏。

The aim of the game is to try and guess the magic word. 游戏的目的是尝试猜测魔术词。

I was wondering how to loop it so that if you got the question wrong, you could have another attempt, and if you get it right, you can move on to level 2. 我想知道如何循环它,以便如果你的问题出错,你可以再次尝试,如果你做对了,你可以继续进行2级。

Any help would be greatly appreciated 任何帮助将不胜感激

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

}

Thanks for the help guys :) 谢谢你的帮助:)

Is this what you want to do 这是你想要做的

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
  }

}

How about this. 这个怎么样。 Using do-while loop. 使用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);
}

You can also start with an infinite loop, then break if magic word has been found. 您也可以从无限循环开始,然后在找到魔术词时中断。

See docs for break and while loop 查看有关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