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?