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

Thread: Guessing game

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing game

    So I have the main part of the code which is letting the user guess a randomly generated number by the computer, but I now need to display how many guesses it took the user to guess the number.

    Code so far:

    import java.util.*;

    class guessing {
    public static void main(String args[]){
    Random number = new Random();
    Scanner input = new Scanner(System.in);

    int computernumber,guess,counter;
    counter = 0;

    computernumber = 1+number.nextInt(20);

    System.out.println("The computer has picked a number from 1 to 20, try and guess it");

    System.out.println("Enter your guess: ");
    guess = input.nextInt();


    while(guess!=computernumber){
    if(guess>computernumber){
    System.out.println("Your guess was high.");
    }else if(guess<computernumber){
    System.out.println("Your guess was low.");
    }else{
    System.out.println("Congratulations, you guessed it");
    }
    System.out.println("Enter your guess: ");
    guess = input.nextInt();
    }
    System.out.println("Congratulations, you guessed it");



    }
    }

    So how would I do it?


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

    Default Re: Guessing game

    Please read the Announcement topic at the top of the sub-Forum and learn how to post your code in code or highlight tags.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Guessing game

    Create a variable and increment it every time the user guesses.

Similar Threads

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