Question
This is a guessing game that randomly selects a number between 1 and 10000.The user will guess what the number is and it will either
This is a guessing game that randomly selects a number between 1 and 10000.The user will guess what the number is and it will either display LOWER or HIGHER. I need help modifying the following code. I need to 'Calculate and display the eligible range of values' after each guess.
If for example, the random number generatedis830and the first value the user enters is450, the range calculated and displayed should be,451 - 10000
If the second value entered is980, then the new range displayed should be,451 - 979.
import java.util.Scanner;
public class GuessingGame {
public static void guess(){
Scanner input = new Scanner(System.in);
int maxNum = 10000;
int minNum = 1;
int rangeNum = maxNum - minNum + 1;
int randNum = (int) (Math.random() * rangeNum);
int guess = 0;
while (guess != randNum) {
System.out.println("Guess a number between 1 and 10000:");
guess = input.nextInt();
if (guess < randNum) {
System.out.println("HIGHER");
System.out.println("From " + guess + " to " + maxNum + ".");
}
else if (guess > randNum) {
System.out.println("LOWER");
System.out.println("From " + guess + " to " + minNum + ".");
}
else {
System.out.println("WINNER");
}
}
}
public static void main(String[] args) {
guess();
}
}
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