Assignemnt #66 and Hi Lo with Limited Tries
Code
/// Name: Raff Lisbona
/// Period: 6
/// Program Name: Hi Lo with Limited Tries
/// File Name: HiLoLimited.java
/// Date Finished: 12/1/2015
import java.util.Scanner;
import java.util.Random;
public class HiLoLimited
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int choice = 1 + r.nextInt(100);
int guess, count = 0;
System.out.println("I'm thinking of a number between 1-100. You have 7 guesses...");
System.out.print("First guess: ");
guess = keyboard.nextInt();
count++;
while ( guess != choice && count < 7 )
{
if ( guess > choice )
{
System.out.println("Sorry, you are too high my friend.");
System.out.print("Guess # " + (count + 1) + ": ");
guess = keyboard.nextInt();
}
else if ( guess < choice )
{
System.out.println("Sorry, that guess is too low.");
System.out.print("Guess # " + (count + 1) + ": ");
guess = keyboard.nextInt();
}
count++;
}
if ( guess == choice )
System.out.println("You guessed it! What are the odds?!?");
else if ( count > 7 )
System.out.println("Sorry, You didn't guess it in 7 tries. The number was " + choice + ". You lose.");
}
}
Picture of the output