Assignemnt #65, Number-Guessing with a Counter
Code
/// Name: Alex Pinder
/// Period: 5
/// Program Name: Number-Guessing with a Counter
/// File Name: prog65.java
/// Date Comlete: 1/12/16
import java.util.Random;
import java.util.Scanner;
public class prog65
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int answer = 1 + r.nextInt(10);
int guessNumber = 1;
int guess;
System.out.println( "I'm thinking of a number between 1 and 10. Try to guess it!" );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
guessNumber++;
while ( guess != answer )
{
System.out.println( "That is incorrect. Guess again." );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
guessNumber++;
}
System.out.println( "Thats Right! You're a good guesser.\nIt only took you " + guessNumber +" times!" );
}
}
Picture of the output