Question
JAVA For this project, I have written a code that initiates a guessing game where the computer takes a range from the user and guesses
JAVA
For this project, I have written a code that initiates a guessing game where the computer takes a range from the user and guesses random numbers that the user indicates if it is high, low or correct, and updates its range according to these prompts. I have written all of this in my main, but need to split it up into 3-4 separate methods that interact with each other. My main should only include any initializing variables and calling the methods necessary.
Please split the code up into separate methods as described above. Included is the code and general expected output, for help. Do not change anything written in the code, simply organize it into methods and reduce any redundancy.
import java.util.Random; import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { while (true) { Scanner sc = new Scanner(System.in); System.out.println("Pick a number for me to guess between any range."); System.out.println("Enter a lower limit and an upper limit"); /*Reading the lower limit and upper limits */ int lower = sc.nextInt(); int upper = sc.nextInt(); System.out.println("Enter the number for the system to guess. (To confirm number is in range)."); /*Read the User Number which will used to check whether the system guess is correct or not */ int userNum = sc.nextInt(); /*CHeck the userNum is with the limit range */ if (userNum >= lower && userNum <= upper) { /*Create object for Random class*/ Random rand = new Random(); while (true) { /*Getting the random number within the range */ int randomNum = lower + rand.nextInt(upper - lower + 1); System.out.println("Computer guesses: " + randomNum); System.out.println("Player responds: this is (high, low, correct):"); /*Reading the player responce */ String playResponse = sc.next(); if (playResponse.equals("high")) { /*If player response is high we need to assign randomNum to upperlimit */ upper = randomNum - 1; } else if (playResponse.equals("low")) { /*If player response is high we need to assign randomNum to lowerlimit */ lower = randomNum + 1; } else if (playResponse.equals("correct")) { System.out.println("Computer says: Good game, Sara!"); /*If guess is correct close the game */ break; } } System.out.println("Play again?"); String playResponse = sc.next(); /*If user Don't want the game again terminate it otherwise starts the game again */ if (playResponse.startsWith("N") || playResponse.startsWith("n")) { break; } } else { System.out.println("Enter Guess between the lower and upper limit only"); } } } }
GENERAL EXPECTED OUTPUT
Player is thinking of a number between 1 and 100.
Computer guesses: 60
Player responds, this is (high, low, correct): high
Computer guesses: 25
Player responds, this is (high, low, correct): high
Computer guesses: 10
Player responds, this is (high, low, correct): low
Computer guesses: 18
Player responds, this is (high, low, correct): correct
Computer says: Good game, Dave!
Play again? Yes
Player is thinking of a number between 1 and 100.
[]
Play again? Not right now
Thanks for playing!
Thank you for your help!
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