Assignemnt #66 Hi-Lo with Limited Tries
Code
Name: Iaroslav Titov
Period: 7
Project Name: HiLoLimTr
File Name: HiLoLimTr.java
Date: 11/10/2015
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class HiLoLimTr
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
byte attempts = 0;
byte guess = 0;
byte SN = (byte)(1 + r.nextInt(100));
System.out.print("I'm thinking of a number from 1 to 100. You have 7 attempts to guess it.\n");
try
{
while (attempts < 7 && SN!=guess)
{
attempts++;
System.out.print("Guess #"+attempts+" ");
guess = (byte) keyboard.nextInt();
if (SN == guess) System.out.println("You guessed it!!! You must have cheated somehow!");
else if (SN < guess) System.out.println("Nope, you guessed too high!");
else System.out.println("Nope, you guessed too low!");
System.out.println();
}
if (attempts >= 7 && SN!=guess) System.out.println("Out of attempts, sorry.(");
}
catch (InputMismatchException e)
{
System.out.println("WRONG INPUT!!!");
}
System.out.println();
}
}
Picture of the output