Question
Hello, I need help with creating a program. I have started working on it, and I have some code, but it's not doing everything it
Hello, I need help with creating a program.
I have started working on it, and I have some code, but it's not doing everything it needs to.
This is the question that describes the program to be created:
Write a Java program to do the following:
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 (inclusive) and then repeatedly prompt the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or chooses to quit. Use a sentinel value (it can be any character of your choice) to determine whether the user wants to quit. Count the number of guesses, and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing games until the user chooses to stop.
I have pasted my code below. If you're able to run it on your machine too and see where I can make changes, and where I can include stuff, that would be so appreciated.
Or if there's an entirely different way to solve this, I'm open to seeing how you go about solving it with some explanations if you don't mind. Thank you so much for your help:
public static void main(String[] args) { String stop = "X"; do { Scanner read = new Scanner(System.in); System.out.println("Welcome to Guess that number. You can quit any time by pressing X ."); Random rand = new Random(); int randomNum = rand.nextInt(100) + 1; while (true) //they can quit at any time with 0 play again? { //Ask user to guess a number System.out.print("Guess a number from 1 to 100: "); //create a scanner int playerNum= read.nextInt(); System.out.println("You Guessed: " + playerNum); //Random number generator System.out.println(randomNum); int count = 0; if (playerNum < randomNum) { System.out.println(" The number you guessed is wrong. Go higher"); count++; continue; } else if (playerNum > randomNum) { System.out.println(" The number you guessed is wrong. Go lower"); count++; continue; } else if (playerNum == randomNum) { //If correct, correct! ask to play again or end game System.out.println(" You got it!"); break; } } String endGame= read.nextLine(); if (stop.equals(endGame)) { System.out.println("Thanks for playing"); break; } }while(true); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started