Assignment #72 Again with the Number-Guessing
Code
/// Name: John Huh
/// Period: 6
/// Program Name: Again with the Number-Guessing
/// File Name: AgainGuess.java
/// Date Finished: 1/7/2016
import java.util.Random;
import java.util.Scanner;
public class AgainGuess
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess, answer, tries;
answer = 1 + r.nextInt(10);
tries = 1;
System.out.println();
System.out.println( "I'm thinking of a number from 1 to 10." );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
System.out.println();
do
{
System.out.println( "That is incorrect. Guess again." );
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
System.out.println();
tries++;
} while ( guess != answer );
if ( guess == answer )
{
System.out.println( "That's right! My secret number was " + answer + "!" );
System.out.println( "It only took you " + tries + " tries." );
}
System.out.println();
}
}
Picture of the output