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

Thread: Guessing Game

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing Game

    public static void main(String[] args)
       {
           Scanner in = new Scanner(System.in);
           System.out.print("Enter your guess:  ");
           int input = in.nextInt();
     
           int comp = (int)(Math.random()*100) + 1;
            if(input < comp)
            {
                System.out.println("Too low");
            }
            else if(input > comp)
            {
                System.out.println("Too High");
            }
     
            else
            {
                System.out.println("Congratulations");
            }
            System.out.println("The random number was:  " + comp);
     
     
     
     
     
     
     
     
        }

    The user is supposed to keep on guessing the number if he doesn't get it correct, but my code doesn't let the person continue to guess.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Guessing Game

    What does your program need if you want to do something over and over again?
    Improving the world one idiot at a time!

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Guessing Game

    ... nvm

    move along, nothing to see here

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    What you need is a loop. For example you can use a while loop like this:
    while(input!=comp){}

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    You can add a do {} while loop. A do {} while loop will repeat through the code once and then again and again until the while is satisfied, for your code it would look like this:
    public static void main(String[] args)
       {
           Scanner in = new Scanner(System.in);
           int comp = (int)(Math.random()*100) + 1;
           int input = 0;
           do {
                System.out.print("Enter your guess:  ");
                input = in.nextInt();
     
                if(input < comp)
                {
                    System.out.println("Too low");
                }
                else if(input > comp)
                {
                    System.out.println("Too High");
                }
     
                 else
                {
                    System.out.println("Congratulations");
                }
                System.out.println("The random number was:  " + comp);
            } while (input != comp);
    }

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    Hello loreneli113,
    The thing you need is called a cycle. More about the types of cycles you can read here.
    Quote Originally Posted by loreneli113 View Post
    public static void main(String[] args)
       {
           Scanner in = new Scanner(System.in);
           System.out.print("Enter your guess:  ");
           int input = in.nextInt();
     
           int comp = (int)(Math.random()*100) + 1;
            if(input < comp)
            {
                System.out.println("Too low");
            }
            else if(input > comp)
            {
                System.out.println("Too High");
            }
     
            else
            {
                System.out.println("Congratulations");
            }
            System.out.println("The random number was:  " + comp);
     
     
     
     
     
     
     
     
        }

    The user is supposed to keep on guessing the number if he doesn't get it correct, but my code doesn't let the person continue to guess.

  7. #7
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: Guessing Game

    How I miss the good old days when you were making a guessing game for the first time. Try using loops, like "for" and "while".

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Guessing Game

    for(ever)/while(true)/do...until(hell freezes over)

    @OP: this thread needs a break condition! Your response is that trigger that allows it to move forward.

    In fact, Junky asked a question and we are yet to hear a response. I conclude (to borrow a phrase from Peter Sellers) that this thread is either dead or... very, very good at holding its breath.

Similar Threads

  1. Guessing game help
    By Norm in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2013, 07:54 AM
  2. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  3. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  4. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  5. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM