Assignemnt #72 Again with the Number-Guessing
Code
Name: Iaroslav Titov
Period: 7
Project Name: NGGWC2
File Name: NGGWC2.java
Date: 1/11/2015
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class NGGWC2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
byte SN = (byte)(1 + r.nextInt(10));
byte guess = 0;
System.out.println("Try to guess the secret number from 1 to 10!!!");
System.out.println();
int attempts = 0;
try
{
do
{
System.out.print("Your guess is: ");
guess = keyboard.nextByte();
attempts++;
if (guess < 1 || guess > 10) System.out.println ("Are you stupid? Between 1 and 10, including them.");
if (guess == SN) System.out.println("\nYou guessed!!! The number is "+SN+". It took you "+attempts+" attempts to guess.");
else System.out.println("You are wrong!!! Try again.");
}
while (guess!=SN);
}
catch(InputMismatchException e)
{
System.out.println("WRONG INPUT!!!");
guess = 0;
}
}
}
Picture of the output