/* * Design and implement an application that plays the Hi-Lo guessing * game with numbers. The program should pick a random number * between 1 and 100, and then repeatedly prompt the * user to guess the number. On each guess, report to the user that * he/she is correct or that the guess is too high or low. Continue * accepting guesses until the user guess correctly or quits. At the end * of the game, ask the user if he/she wants to play again. */ package chapterFour; import java.util.Scanner; import java.util.Random; public class problem8 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random rand = new Random(); String playAgain = "y"; int num = 0, randomNumber, guesses = 0; randomNumber = rand.nextInt(100) + 1; while(playAgain.equalsIgnoreCase("y")); { while(num != randomNumber) { System.out.print("Enter a random number between 1 and 100 to guess : "); num = scan.nextInt(); if(num > randomNumber) System.out.println("Too high"); if(num < randomNumber) System.out.println("Too low"); else if(num == randomNumber) System.out.print("Correct, "); guesses++; } System.out.println("It took you " + guesses + " guesses to get it! "); System.out.println("Play again? (y/n)"); playAgain = scan.nextLine(); } } }