Code
/// Name: Afaf Nabeeha
/// Period: 7
/// Program Name: Number-Guessing with a Counter
/// File Name: Counter.java
/// Date Finished: 12/14/2015 public class Counter
import java.util.Random;
import java.util.Scanner;
public class Counter
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int randomNumber = 1 + r.nextInt(10);
int guess, tries;
tries = 0;
System.out.println( "I have chosen a number between 1 and 10. Try to guess it. " );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
tries++;
while ( randomNumber != guess )
{
System.out.println( "That is incorrect. Guess again. " );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
tries++;
}
System.out.println( "That's right! Your a good guesser. " );
System.out.println( "It only took you " + tries + " tries. " );
}
}
Picture of the output