import java.util.Locale; import java.util.Random; import java.util.Scanner;
//GUESSING GAME - JAVA public class GuessingGameChallenge { public static void main(String[] args) { //Variables //1.Create an instance for random class //Range 0-20 int winnerNumber = new Random().nextInt(20); Scanner keyboard = new Scanner(System.in).useLocale(Locale.US); int userTries = 0; int userNumber = 0;
//2.Create a loop to control tries
while (userTries <= 5) {
if (userTries == 0) {
System.out.println("Welcome to Guessing Game");
}
//Save user input value
System.out.println("Please type a number");
userNumber = keyboard.nextInt();
userTries++;
//3. Condition to check if input value is the winner number
if (userNumber == winnerNumber) {
System.out.printf("Congrats! You're the winner with %d tries and winner number %d ", userTries, winnerNumber);
break;
} else if (userTries == 5 && userNumber != winnerNumber) {
System.out.println("GAME OVER");
break;
} else if (userNumber < winnerNumber) {
System.out.println("Sorry, but the number is higher");
} else {
System.out.println("Sorry, but the number is less");
break;
}
}
}
}