Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Broken loop - number guessing game.

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Broken loop - number guessing game.

    I know I'm not the first to post this, but I've spent hours scouring other people's code, and I can't figure out what's wrong with mine. It says 'you win' even when I guess an incorrect number. And then the 'win' message keeps repeating. I'm thinking I need a way to generate a new input? And then maybe take the 'win' message out of the loop, but that breaks it too. Help?

    Screen Shot 2014-06-03 at 8.03.55 AM.jpg


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Broken loop - number guessing game.

    Oh since that screen shot wants to be tiny, here's the text:

    /**
    * Emily Caraballo's Guess a Number Game, v1.0
    */

    import javax.swing.JOptionPane;
    import java.util.Random;

    public class GuessNumber
    {
    public static void main(String[] args)
    {
    Random rand = new Random();
    int randomNo = rand.nextInt(100) + 1; // Random Number Generated
    int guess; // Number user Guesses
    int attempts = 0; // Number of guesses

    guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number 1-100"));
    attempts++;

    while (guess !=randomNo) {

    if (guess < randomNo)
    {
    JOptionPane.showInputDialog(null, "Too low sucker! Guess a higher number.");
    }

    else if (guess < randomNo)
    {
    JOptionPane.showInputDialog(null, "Woah! Guess a lower number.");
    }

    else if (guess <1 || guess >100)
    {
    JOptionPane.showInputDialog(null, "Really? Did You read the instructions? " +
    "That number is not between 1-100, guess again.");
    }


    JOptionPane.showMessageDialog(null, "You win!");
    JOptionPane.showMessageDialog(null, "The number was " + randomNo);
    JOptionPane.showMessageDialog(null, "You guessed in " + attempts + " tries.");
    }

    } // end main
    } // end class GuessNumber

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Broken loop - number guessing game.

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Broken loop - number guessing game.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Broken loop - number guessing game.

    Thanks Greg & Norm, this looks much better.

    /** 
     * Emily Caraballo's Guess a Number Game, v1.0
     */
     
    import javax.swing.JOptionPane;
    import java.util.Random;
     
    public class GuessNumber
    {
    public static void main(String[] args)
       {
          Random rand = new Random(); 
          int randomNo = rand.nextInt(100) + 1;  // Random Number Generated
          int guess;                             // Number user Guesses
          int attempts = 0;                      // Number of guesses
     
          guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number 1-100"));
          attempts++;
     
         while (guess !=randomNo)
    	 	{
     
             if (guess < randomNo)
             {
                JOptionPane.showInputDialog(null, "Too low sucker! Guess a higher number.");
             }
     
             else if (guess < randomNo)
             {
                JOptionPane.showInputDialog(null, "Woah! Guess a lower number.");
             }
     
             else if (guess <1 || guess >100)
             {
                JOptionPane.showInputDialog(null, "Really? Did You read the instructions? " +
                "That number is not between 1-100, guess again.");
             }   
     
     
          JOptionPane.showMessageDialog(null, "You win!");
          JOptionPane.showMessageDialog(null, "The number was " + randomNo);
          JOptionPane.showMessageDialog(null, "You guessed in " + attempts + " tries.");
          }     
     
       } // end main
    } // end class GuessNumber

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Broken loop - number guessing game.

    It says 'you win'
    Is the code that prints that message inside the while() loop? Where is there any code to prevent the message from printing every time the loop goes around?

    I need a way to generate a new input? And then maybe take the 'win' message out of the loop
    Yes both those guesses are correct. Those changes need to be made for the code to work properly.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Broken loop - number guessing game.

    Moving it outside the loop, and including new input. Now, program doesn't allow input at all, and generates 4 errors.
    Thanks for your patience!!

     
    /** 
     * Emily Caraballo's Guess a Number Game, v1.0
     */
     
    import javax.swing.JOptionPane;
    import java.util.Random;
     
    public class GuessNumber
    {
    public static void main(String[] args)
       {
          Random rand = new Random(); 
          int randomNo = rand.nextInt(100) + 1;  // Random Number Generated
          int guess;                             // Number user Guesses
          int attempts = 0;                      // Number of guesses
     
          guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number 1-100"));
          attempts++;
     
         while (guess !=randomNo)
    	 	{
     
             if (guess < randomNo)
             {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Too low sucker! Guess a higher number.""));
             }
     
             else if (guess < randomNo)
             {
                JOptionPane.showMessageDialog(null, "Woah! Guess a lower number.");
             }
     
             else if (guess <1 || guess >100)
             {
                JOptionPane.showMessageDialog(null, "Really? Did You read the instructions? " +
                "That number is not between 1-100, guess again.");
             }   
     
          }
     
          JOptionPane.showMessageDialog(null, "You win!");
          JOptionPane.showMessageDialog(null, "The number was " + randomNo);
          JOptionPane.showMessageDialog(null, "You guessed in " + attempts + " tries.");
     
     
       } // end main
    } // end class GuessNumber

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Broken loop - number guessing game.

    generates 4 errors.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Where is the value of guess changed inside of the loop? If the value does not change the loop will go forever and do the same thing.

    Add some code inside the loop to change the value of guess.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Broken loop - number guessing game.

     ----jGRASP exec: javac -g GuessNumber.java
    GuessNumber.java:25: error: unclosed string literal
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Too low sucker! Guess a higher number.""));
                                                                                                                   ^
    GuessNumber.java:25: error: ';' expected
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Too low sucker! Guess a higher number.""));
                                                                                                                       ^
    GuessNumber.java:28: error: 'else' without 'if'
             else if (guess < randomNo)
             ^
    GuessNumber.java:47: error: reached end of file while parsing
    } // end class GuessNumber
     ^
    4 errors
     
     ----jGRASP wedge: exit code for process is 1.
     ----jGRASP: operation complete.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Broken loop - number guessing game.

    Look at where the ^ beneath the statement line is pointing.
    How did that extra " get in the code? It wasn't there in the first post.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    emilysk8 (June 3rd, 2014)

  12. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Broken loop - number guessing game.

    You can also take a look at the syntax highlighting in the code tags in your own post.
    You might see that there is quite a bit of blue color in the lower part of the while-loop. Maybe this might help you a little.

  13. The Following User Says Thank You to Cornix For This Useful Post:

    emilysk8 (June 3rd, 2014)

  14. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Broken loop - number guessing game.

    <3
    You're my hero today. You give me hope that java is crackable. THANK YOU!!!

Similar Threads

  1. [SOLVED] Random number guessing game with three tries (loops)
    By ATB in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 8th, 2014, 07:30 PM
  2. Basic Number guessing game.
    By Ddng940 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2012, 08:01 PM
  3. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM