Assignemnt #66 and Hi-Lo with Limited Tries
Code
/// Name: Kyle Ivy
/// Period: 5
/// Program Name: HiLo
/// File Name: HiLo.java
/// Date Finished: 1/5/2016
import java.util.Scanner;
import java.util.Random;
public class HiLo {
public static void main( String[] args ) {
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int guesses = 1, MaxGuesses = 7, guess;
int rnumber = 1 + r.nextInt(100);
System.out.println( "I'm thinking of a number between 1-100. You have 7 guesses. ");
System.out.print( "First guess: ");
guess = keyboard.nextInt();
while ( guesses < MaxGuesses && guess != rnumber )
{
if ( guess < rnumber){
System.out.println("Too low.");
}
else if ( guess > rnumber ){
System.out.println("Too high");
}
guesses++;
System.out.print( "Guess # " + guesses + " " );
guess = keyboard.nextInt();
}
if ( guess == rnumber )
System.out.println( "You guessed it! What are the odds?!? ");
else System.out.println("Sorry, you didn't guess it in 7 times. ");
}
}
prog66.PNG