Assignment #55 A Number-Guessing Game
Code
/// Name: John Huh
/// Period: 6
/// Program Name: A Number-Guessing Game
/// File Name: Guess.java
/// Date Finished: 11/12/2015
import java.util.Random;
import java.util.Scanner;
public class Guess
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess, answer;
answer = 1 + r.nextInt(10);
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();
if ( guess != answer )
{
System.out.println( "Sorry, but I was really thinking of " + answer + "." );
}
else if ( guess == answer )
{
System.out.println( "That's right! My secret number was " + answer + "!" );
}
System.out.println();
}
}
Picture of the output