سؤال

I'm new to Java and this site, and have made a simple guessing game.

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.

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.

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

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top