Assignemnt 55 and A Number-Guessing Game
Code
/// Name: Xinting Chen
/// Period: 7
/// Program Name: NumberGuessingGame
/// File Name: NumberGuessingGame.java
/// Date Finished: 12/3/2015
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class NumberGuessingGame
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int SecretNumber = (byte)(1 + r.nextInt(10));
int guess = 0;
System.out.println("I'm thinking of a number from 1 to 10!");
System.out.println();
try
{
System.out.print("Your guess: ");
guess = keyboard.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("WRONG INPUT!");
guess = 0;
}
if (guess == SecretNumber) System.out.println("You are right! mm number is "+ SecretNumber +".");
else System.out.println("Sorry, but I was really thinking of " + SecretNumber + ".");
}
}
Picture of the output