0

The program is a game where the user has to guess the magical number to win. I'm new to java, and I am still learning. When I type in 53, it just ask the question agains instead of displaying the output. I'm sure it's a simple error that I'm just not catching. Thanks!

import javax.swing.JOptionPane;
class NumberFrom1To100 {
    public static void main(String[] args) {

        boolean stillplaying = true;
        double answer = 53;
        double userAnswer;
        String userInput;

        while (stillplaying == true) {

            userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100.");
            userAnswer = Double.parseDouble(userInput);

            while (userAnswer != 53) {
                if (userAnswer < 53) {
                    JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again.");
                    break;
                } else if (userAnswer > 53) {
                    JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again.");
                    break;
                } else if (userAnswer == 53) {
                    JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!");
                    break;
                }
            }
        }
        /* System.exit(0); */
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Jeremy
  • 59
  • 6

2 Answers2

2

The inner while loop is not only not needed, it is also breaking your program. Two scenarios, either userAnswer is 53 or not. Let's check those two scenarios.

Not equal to 53 will trigger the while loop. If it is less than, equal to (which it never can be), or greater than 53 it will break. Which renders a loop obsolete.

Equal to 53 won't trigger the loop at all, which is the result you are experiencing now.

import javax.swing.JOptionPane;

class NumberFrom1To100
{
    public static void main(String[] args) {

        boolean stillplaying = true;
        double answer = 53;
        double userAnswer;
        String userInput;

        while (stillplaying) {
            userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100.");
            userAnswer = Double.parseDouble(userInput);

            if (userAnswer < 53) {
                JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again.");
            } else if (userAnswer > 53) {
                JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again.");
            } else {
                JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!");
            }
        }

        // System.exit(0);
    }
}

I do not know how you want to handle "victory", you can set stillplaying to false in that comparison.

I also took the liberity of removing your comparison to true in the outer while loop as it does nothing. It is also common to name the variable with lowerCamelCase => stillPlaying which could only be called playing, a fairly common name of the boolean-game loop variable.

Emz
  • 1,280
  • 1
  • 14
  • 29
0

Your code

else if (userAnswer == 53) { JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!");

will never be reached because you have put it inside

while (userAnswer != 53) {.....}

Just remove this inner while loop with it's respective parentheses and it should be good.

Correct code will be:

import javax.swing.JOptionPane;

class NumberFrom1To100 { public static void main(String[] args) {

    boolean stillplaying = true;
    double answer = 53;
    double userAnswer;
    String userInput;

    while (stillplaying == true) {

        userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100.");
        userAnswer = Double.parseDouble(userInput);


            if (userAnswer < 53) {
                JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again.");

            } else if (userAnswer > 53) {
                JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again.");

            } else if (userAnswer == 53) {
                JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!");

            }

    }``
    /* System.exit(0); */
}

}

justLearning
  • 299
  • 2
  • 7
  • The `break`s in the `if` statements will quite literally break the application. You also have some formatting problems in the answer. – Emz Oct 29 '15 at 02:05