Assignemnt #66 - Guessing Limits
Code
/// Name: Caelan Dorstad
/// Period: 6
/// Program Name: GuessinWithLimits
/// File Name: GuessinWithLimits.java
/// Date Finished: 1/5/2016
import java.util.Scanner;
import java.util.Random;
public class GuessinWithLimits{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int a, num = 1 + r.nextInt(100);
int tries = 0;
int maxtry = 8;
System.out.print("GUESS 0-100. You got 8 tries.: ");
a = keyboard.nextInt();
tries++;
while ( a != num && tries < maxtry ){
if ( a < num){
System.out.println("Guess higher: ");
a = keyboard.nextInt();
}
else if ( a > num){
System.out.println("Guess lower: ");
a = keyboard.nextInt();
}
tries++;
}
if ( a == num){
System.out.println("Yes! It only took you " + tries + " times.");
}
else if ( tries >= maxtry){
System.out.println("Sorry, you didn't guess it in 8 tries. You lost.");
}
}
}
Picture of the output