Assignemnt #66 and GuessWithLimits.java
Code
/// Name: Boris Kazantsev
/// Period: 6
/// Program Name: GuessWithLimits
/// File Name: GuessWithLimits.java
/// Date Finished: 1/14/2016
import java.util.Scanner;
import java.util.Random;
public class GuessWithLimits{
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