While Loops #66 (Now with a secret level included! -1)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name:Limit Shot Hi Lo
/// File Name:LimitShotHiLo.java
/// Date Finished: 12/8/2015
import java.util.Random;
import java.util.Scanner;
public class LimitShotHiLo
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner input = new Scanner(System.in);
int x = 1 + r.nextInt(100), guess;
int counter = 1;
int limit = 7;
System.out.println("");
System.out.print("I'm thinking of a number between 1-100. You have 7 guesses.");
System.out.println("");
System.out.print("First guess ");
guess = input.nextInt();
counter++;
System.out.println("");
if ( guess != x && guess == 0) {
System.out.println("You've sucessfully inputed the secret number");
System.out.println("");
System.out.print("Welcome to HARD mode. You now only have 1 try to guess the number. GOOD LUCK!");
guess = input.nextInt();
System.out.println("");
if (guess == x) {
System.out.println("You guessed it! What are the odds?!?");
} if (guess > x) {
System.out.println("Sorry, you are too low. I was thinking of " + x);
}
if (guess < x) {
System.out.println ("Sorry, you are too high. I was thinking of " + x);
} if (guess == x) {
System.out.println("That's right! My secret number was " + x + "!");
}
System.exit(0);
}
while ( guess != x && counter < limit + 1) {
if ( guess < x) {
System.out.println("Sorry, you are too low.");
System.out.print("Guess # " + counter + ": ");
guess = input.nextInt();
counter++;
System.out.println("");
}
if ( guess > x){
System.out.println("Sorry, you are too high.");
System.out.print("Guess # " + counter + ": ");
guess = input.nextInt();
counter++;
System.out.println("");
}
}
if ( guess == x) {
System.out.println(" You guessed it! What are the odds?!? ");
}
if ( counter > limit && guess != x){
System.out.println(" Sorry, you didn't guess it in 7 tries. You lose. ");
}
}
}
Picture(s) of the output