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

Thread: Counter on Hi-Lo game

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

    Default Counter on Hi-Lo game

    How can i get a counter then display the number of attempts they have done withing my game after each right or wrong answer?

    Here is my code:


    /********************
    Hilo Game
    ********************/
    import java.util.Random;
    import java.util.Scanner;


    public class RandomGen2
    {
    public static void main (String[] args)
    {
    final int MAX = 100;
    int answer;
    int guess;
    int counter;

    Scanner Keyboard = new Scanner(System.in);
    System.out.print ("I'm thinking of a number between 1 and " + MAX + ". Guess what it is: (or type -999 to quit) ");
    guess = Keyboard.nextInt();

    Random generator = new Random(); //Random generator
    answer = generator.nextInt(MAX) +1;

    if (guess == answer){ //If user guesses right
    System.out.println ("You Win!!");

    }
    else if (guess == -999){ //End the game
    System.out.println ("You have ended your game");

    }
    while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

    if (guess > answer && guess != 0){ //If guess is higher than answer
    System.out.println ("ERROR- Your Guess is out of the range of 0 "+ MAX );
    guess = Keyboard.nextInt();

    }
    else{
    if (guess < answer && guess != 0){ //If guess is lower than answer
    System.out.println ("Too low. Try Again!");
    guess = Keyboard.nextInt();
    }

    else if (guess == answer){ //If guess equals answer
    System.out.println ("You got it! Good guessing!");
    }
    else if (guess == 0){ //Game ends
    System.out.println ("You have ended your game. Goodbye.");
    }
    }
    }
    if ( guess == answer){
    System.out.println ("You got it! Good guessing!");
    }
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Counter on Hi-Lo game

    Each time they make a guess increment counter.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    gdoggson (October 21st, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Counter on Hi-Lo game

    i did this and it gives me a error message "error: variable counter might not have been initialized"


    if (guess > answer){ //If guess is higher than answer
    System.out.println ("ERROR- Your Guess is out of the range of 0 - " + MAX );
    guess = Keyboard.nextInt();
    counter++;

    }
    else{
    if (guess < answer){ //If guess is lower than answer
    System.out.println ("Too low. Try Again!");
    guess = Keyboard.nextInt();
    counter++;
    }

    else if (guess == answer){ //If guess equals answer
    System.out.println ("You Win" + "\nYou Guessed in " + counter + " attempts");
    counter++;
    }

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Counter on Hi-Lo game

    Quote Originally Posted by gdoggson View Post
    "error: variable counter might not have been initialized"
    Then initialise it! If you don't know what that means then Google it, read your textbook, read your lecture notes.
    Improving the world one idiot at a time!

Similar Threads

  1. 1-100 counter
    By david625 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2013, 06:34 AM
  2. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 04:32 AM
  3. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 07:50 AM
  4. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 10:56 AM