2

I'm having an issue I haven't ran into yet. I wrote a guessing game in java to guess a number between 1 and 1000. The program should loop until the player decides he or she no longer wants to play. I can not get it to loop however. If I take out the Scanner class at the bottom of the program it loops fine but I need the scanner class to complete the program as required in my class. If the user types "no" in the scanner class the while loop variable is set to false and the program terminates just as it should. But I cannot get it to loop if the user chooses "yes" and the while loop variable remains true. After "yes" is typed the program just pauses and nothing happens, and the program doesn't end. Any help would be greatly appreciated.

import javax.swing.*;
import java.util.*;
public class RandomGuess3
{
   public static void main(String[] args)
   {
      String userGuess;
      String playAgain;
      int guessNum;
      int randomNum = (1+(int)(Math.random()*1000));
      int guessCount = 0;
      boolean correct = false;
      boolean startGame = true;
      boolean newGame = false;

         if(launchGame() == true)
            startGame = true;
         while(startGame == true)
         {
            do
            {
               userGuess = JOptionPane.showInputDialog(null,
                  "I'm thinking of a number between 1 and 1000."
                  +"\nEnter your guess:", "Guessing Game",
                  JOptionPane.QUESTION_MESSAGE);
               guessNum = Integer.parseInt(userGuess);
               if(guessNum != randomNum)
                  if(guessNum < randomNum)
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too low");
                  else
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too high");
               if(guessNum == randomNum)
                  correct = true;

                  ++guessCount;
            }while(guessNum != randomNum);

            if(correct == true)
                  JOptionPane.showMessageDialog(null, "CONGRATULATIONS!"
                     +"\nYou guessed correctly!"
                     +"\nAnd it only took "+guessCount+" tries.");

            Scanner input = new Scanner(System.in);      
            System.out.print("Want to play again? Enter yes or no >> ");
            playAgain = input.nextLine().toUpperCase();

            if(playAgain.equals("NO"))
               startGame = false;
            else
               startGame = true;
         }

   }
   public static boolean launchGame()
   {
      int letsPlay;
      boolean startGame;
      letsPlay = JOptionPane.showConfirmDialog(null,
         "Want to play a guessing game?");
      startGame = (letsPlay == JOptionPane.YES_OPTION);
      System.out.println("startGame is "+startGame);
      return startGame;
   }

}
5
  • 1
    Do not compare booleans as "someBoolean == true". This is just confusing and can lead to errors. Use a simple "if(someBoolean)" or "if(!someBoolean)". Commented Jun 28, 2015 at 22:40
  • I ran your code and it seems to work fine for me, apart from the to-be-guessed number staying the same between runs (and it's kind of hard to get the 1/1000 guess right :) ). Did you edit out some usage of Scanner? Commented Jun 28, 2015 at 22:47
  • The yes/no works fine for me too. There are however other bugs... If you choose 'yes', then randomNum remains the same - you should assign a new value before entering the outer loop once again. The same goes for guessCount, it should be reassigned to 0. Moreover, mixing Swing and System.in is very confusing. Commented Jun 28, 2015 at 23:05
  • Thanks! On the second round the JOptionPane was hidden behind my editor window and I didn't see it / thought it wasn't working. Simple mistake. Commented Jun 29, 2015 at 0:32
  • To make randomize the number in subsequent rounds and clear the count I declared randomNum = (1+(int)(Math.random()*1000)); and guessCount = 0; after the while loop and before the do loop. Commented Jun 29, 2015 at 0:34

1 Answer 1

1

You could take out the

if(launchGame() == true)
   startGame = true;

entirely, maybe using this:

boolean startGame = launchGame();             
while(startGame)
             {
                do
                {
                   userGuess = JOptionPane.showInputDialog(null,
                      "I'm thinking of a number between 1 and 1000."
                      +"\nEnter your guess:", "Guessing Game",
                      JOptionPane.QUESTION_MESSAGE);
                   guessNum = Integer.parseInt(userGuess);

Another potential structural improvement could be using the natural if else structure of the game. "if the guess was wrong, tell them lower or higher, else tell them they won and offer to play again" is really what it seems the game should do.

Also, your correct boolean is sort of redundant, because to exit the loop, correct MUST be true, so you don't need to check again if correct.

So:

                  if(guessNum < randomNum)
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too low");
                  else
                     JOptionPane.showMessageDialog(null, "Sorry you guessed too high");
                  ++guessCount;
            }while(guessNum != randomNum);

            JOptionPane.showMessageDialog(null, "CONGRATULATIONS!"
                     +"\nYou guessed correctly!"
                     +"\nAnd it only took "+guessCount+" tries.");

The final thing you MAY want to consider, and this is the most preference-based of all, is you may want to check yes or no differently. Usually when I see these sorts of checks, I usually see people grab the first letter of the String response, and do a charAt(0), and if charAt(0) == 'y' or charAt(0) == 'Y', then play again, otherwise you have a no. Some food for thought if you prefer that approach.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I implemented your code improvements. The original code worked and the JOptionPane was hidden behind my JGrasp window..... idiot mistake. Thanks Again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.