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 11 of 11

Thread: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Ok, here's what I got.

     
    import java.util.Random;
    import java.util.Scanner;
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    public class demoGuessGame {
     
        public static void main(String[] args) {
     
            Random any = new Random();
            Scanner input = new Scanner (System.in); 
     
            try {
     
            int maxNum, maxTries, guess;
            boolean win = false;
     
            maxNum = any.nextInt(10);
            maxTries = 0;   
     
     
            while (win == false)
            {
     
                System.out.print("\n" + "Pick a number between 1 and 10: ");
                guess = input.nextInt();
                maxTries++;
     
                if (guess == maxNum)
                {
     
                    win = true;
     
                }
     
                else if (guess <= maxNum)
                {
     
                    System.out.println("\n" + "The number you picked is TOO LOW.");
     
                }
     
                else if (guess >= maxNum)
                {
     
                    System.out.println("\n" + "The number you picked is TOO HIGH.");
     
                }
     
            } 
     
                System.out.println("\n" + "YOU WIN!");
                System.out.println("The number was: " + maxNum);
                System.out.println("It took this many tries to get it right: " + maxTries);
     
            } // end of the try section
     
          catch(Exception msg)
          {
     
        	input.next();
        	System.out.println("\n" + "Sorry, invalid input- " + msg + " exception try again ");
     
          } //end of the catch section
     
        }  
    }


    I want to make it loop again when asked, "Do you want to play again?"

    But I don't know how. Does anyone know how to?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,339
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    You already know how to loop until you win.

    Why don't you do the same thing to loop until you quit?

    Hint: It might help if you put your current code inside a function called doOneGame().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    No, not like that.

    I meant for the program to loop again after you win.

    Like this:

    YOU WIN!
    The number was: #
    It took you this many tries to get it right:

    Do you want to play again? Y/N?:


    Do you know how to get it to work?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,339
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    I understand exactly what you meant.

    The process will be the same. You'll have to use a loop to repeat the process. Putting your existing code in a method named doOneGame() might help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    15
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    make a new boolean called "Again".(=true)
    Then make a while before the program starts. ( While (Again = True)
    after the program finishes you ask the scanner to do Y/N
    Then when they say Yes make the boolean true
    and when they say no make the boolean false!
    Hope this helped!

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Quote Originally Posted by Niels van Ee View Post
    make a new boolean called "Again".(=true)
    Then make a while before the program starts. ( While (Again = True)
    after the program finishes you ask the scanner to do Y/N
    Then when they say Yes make the boolean true
    and when they say no make the boolean false!
    Hope this helped!
    Can you show me by using my code for an example?

    I need to be sure so as I don't get all mixed up.

    You can use my code to help you.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    15
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Quote Originally Posted by LooneyTunerIan View Post
    Can you show me by using my code for an example?

    I need to be sure so as I don't get all mixed up.

    You can use my code to help you.
    Sorry it's kinda hard and i don't want to rewrite the script etc etc. Good luck with the project! but now you know how to do it I suppose. Hope you can figure it out.

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    import java.util.Random;
    import java.util.Scanner;
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    public class demoGuessGame3 
    {
     
        public static void main(String[] args) 
        {
     
            Random any = new Random();
            Scanner input = new Scanner (System.in); 
            boolean again;
     
            while (again = true)
            {
     
            try 
            {
     
            int maxNum, maxTries, guess;
            boolean win = false;
     
            maxNum = any.nextInt(10);
            maxTries = 0;   
     
                while (win == false)
                {
     
                    System.out.print("\n" + "Pick a number between 1 and 10: ");
                    guess = input.nextInt();
                    maxTries++;
     
                    if (guess == maxNum)
                    {
     
                        win = true;
     
                    }
     
                    else if (guess <= maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO LOW.");
     
                    }
     
                    else if (guess >= maxNum)
                    {
     
                        System.out.println("\n" + "The number you picked is TOO HIGH.");
     
                    }
     
            System.out.println("\n" + "YOU WIN!");
            System.out.println("The number was: " + maxNum);
            System.out.println("It took you this many tries to get it right: " + maxTries);
     
            } 
     
          }// end of the try section
     
          catch(Exception msg)
          {
     
        	input.next();
        	System.out.println("\n" + "Sorry, invalid input- " + msg + " exception try again ");
     
          } //end of the catch section
     
       }
     
            }   
    }


    Here's what I got so far.

    What do I do next?
    Last edited by LooneyTunerIan; April 8th, 2014 at 06:13 PM.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Start by reviewing this statement:

    while (again = true)

    Then consider that a boolean does not need to be checked for its equality to 'false' or 'true' in a conditional statement, because it IS false or true. With that in mind,

    while ( again ), and

    while ( !win )

    are sufficient.

  10. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Can you show me by using my code for an example?

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,339
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Guessing Game Demo, wanting to add a doAgain Loop, but I don't know how to.

    Quote Originally Posted by LooneyTunerIan View Post
    Can you show me by using my code for an example?
    He already did.

    This is an assignment statement:

    again = true

    This is a comparison statement:

    again == true

    But you don't need to use explicit comparison on a boolean, just use it directly:

    if(again)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Cocos2Dx programmers needed to finish a game demo
    By yougofar in forum Android Development
    Replies: 0
    Last Post: November 8th, 2013, 02:49 AM
  2. Replies: 9
    Last Post: February 24th, 2013, 05:51 PM
  3. How To Add a Method/Demo?
    By heythisgreg in forum Java Theory & Questions
    Replies: 5
    Last Post: December 15th, 2012, 07:08 AM
  4. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  5. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 03:11 AM

Tags for this Thread