Assignment #55 A Number-Guessing Game
Code
/// Name: Matthew Lorence
/// Period: 5
/// Program Name: GuessAgain
/// File Name: GuessAgain.java
import java.util.Scanner;
import java.util.Random;
public class GuessAgain
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
Random r = new Random();
int number = 1 + r.nextInt(10);
System.out.println("I'm thinking of a number from 1 to 10.");
System.out.print("Your guess: ");
int guess = keyboard.nextInt();
if (guess == number)
{
System.out.println("That's right! My secret number was " + number +"!");
}
else
{
System.out.println("Sorry, but I was really thinking of " + number + ".");
}
}
}
Picture of the output