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
Get step-by-step solutions from verified subject matter experts
