I searched for my query but couldn't find anything of use. I'm just starting to learn Java and I made a basic guessing game program. My problem is that I need to count the number of guesses the user makes and I'm unsure how to do this. I would really appreciate any help you guys could give me. Here is my code so far:
double ran;
ran = Math.random();
int r = (int)(ran*100);
Scanner in = new Scanner (System.in);
int g = 0;
System.out.print("Please make a guess between 1 and 100: ");
g = in.nextInt();
while (g!=r){
if (g<=0){
System.out.print("Game over.");
System.exit(0);
}
else if (g>r){
System.out.print("Too high. Please guess again: ");
g = in.nextInt();
}
else if (g<r){
System.out.print("Too low. Please guess again: ");
g = in.nextInt();
}
}
System.out.print("Correct!");