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!");
}
}
}