# Counter on Hi-Lo game

• October 20th, 2013, 10:38 PM
gdoggson
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 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

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();
}

System.out.println ("You got it! Good guessing!");
}
else if (guess == 0){ //Game ends
System.out.println ("You have ended your game. Goodbye.");
}
}
}
System.out.println ("You got it! Good guessing!");
}
}
}
• October 20th, 2013, 10:40 PM
Junky
Re: Counter on Hi-Lo game
Each time they make a guess increment counter.
• October 20th, 2013, 11:04 PM
gdoggson
Re: Counter on Hi-Lo game
i did this and it gives me a error message "error: variable counter might not have been initialized"

System.out.println ("ERROR- Your Guess is out of the range of 0 - " + MAX );
guess = Keyboard.nextInt();
counter++;

}
else{
System.out.println ("Too low. Try Again!");
guess = Keyboard.nextInt();
counter++;
}