Scanner input = new Scanner(System.in);
int randomNumber = new Random().nextInt(100);
int count = 1;
System.out.println(randomNumber);
for (int i = 0; i < 5; i++) {
String attemptWord = i > 1 ? "attempts" : "attempt";
String attemptWord2 = count > 1 ? "attempts" : "attempt";
if (i >= 1) {
System.out.printf("You have wasted %d %s.%n", i, attemptWord);
}
System.out.println("Try to match a number between 0 and 100: ");
int attemptNumber = input.nextInt();
if (attemptNumber > randomNumber) {
System.out.println("Too high.");
} else if (attemptNumber < randomNumber) {
System.out.println("Too low.");
} else {
System.out.printf("Nice! You got it in %d %s!%nThanks for playing.", count, attemptWord2);
break;
}
if (i == 4) {
System.out.println("Thanks for playing.");
}
count++;
}