Assignemnt #66 and Hi-Lo with Limitied Tries
Code
/// Name: Yordan Rashkov
/// Period: 7
/// Program Name: Hi-Lo with Limitied Tries
/// File Name: HiLoLimitied.java
/// Date Finished: 1/6/2016
import java.util.Random;
import java.util.Scanner;
public class HiLoLimited
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess, x = 1 + r.nextInt(100), maxTries, Counter;
maxTries = 7;
Counter = 0;
System.out.println();
System.out.println();
System.out.println("I'm thinking of a number between 1-100. Try and guess it. You have exactly 7 tries.");
System.out.print(" First guess>: ");
guess = keyboard.nextInt();
Counter++;
System.out.println();
System.out.println();
System.out.println();
while ( guess != x && Counter < maxTries )
{
if ( x < guess )
{
Counter++;
System.out.println(" Sorry the number you have chosen is way too high! ");
System.out.print(" Number of tries: " + Counter );
System.out.println(" ... next guess please: ");
guess = keyboard.nextInt();
System.out.println();
System.out.println();
}
else if ( x > guess )
{
Counter++;
System.out.println(" Sorry the number you have chosen is way too low! ");
System.out.print(" Number of tries: " + Counter );
System.out.println(" ... next guess please: ");
guess = keyboard.nextInt();
System.out.println();
System.out.println();
}
}
if ( guess == x && Counter < maxTries )
{
System.out.println(" I cannot believe it, you got it. ");
System.out.println();
System.out.println();
System.out.println();
}
else if ( guess != x && Counter >= maxTries )
{
System.out.println(" Sorry, you have ran out of tries. You lose! ");
}
}
}