Assignment #66 and Hi-Lo Limit
Code
/// Name: Graham Pollock
/// Period: 5
/// Program Name: HiLoLimit
/// File Name: HiLoLimit.java
/// Date Finished: 12/11/2015/
import java.util.Random;
import java.util.Scanner;
public class HiLoLimit
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int snumber, guess, n;
snumber = 1 + r.nextInt(100);
n = 0;
System.out.println("I'm thinking of a number between 1-100. You have 7 guesses. Good luck!");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
while (guess != snumber && n < 7)
{
if (guess > snumber)
{
System.out.println("You were a little high.");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
}
else if (guess < snumber)
{
System.out.println("You were a little low.");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
}
}
if (guess == snumber)
{
System.out.println("You got it!");
}
else if (n >= 7)
{
System.out.println("Sorry you ran out of tries. you lose.");
}
}
}
Picture of the output