Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, create a random number guessing game that prompts the user for a range

ID: 3879873 • Letter: I

Question

In Java, create a random number guessing game that prompts the user for a range for the program. If user enters 4 and 60, the program generates a random number between 4 and 60. If the user does not enter a range, the default range should be between 1 and 100. If the user enters a number that is too low, the program should return "too low. try again". If the number is too high it should return "too high. try again". The program should also keep track of how many tries it takes for the user to figure out the correct number.

Explanation / Answer

package org.students;

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {

//Declaring variables
int secret, guess, no_of_guesses = 1, upperlimit, lowerLimit;
String str = "";
//Creating the Random class object
Random generator = new Random();
System.out.println("Welcome to the Number Guessing Game");
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the user guess entered by the user
System.out.print("Enter the Lower Limit and upper limit :"); {
str = sc.nextLine();
if (str.isEmpty()) {
lowerLimit = 1;
upperlimit = 100;
} else {
String nos[] = str.split(" ");
lowerLimit = Integer.parseInt(nos[0]);
upperlimit = Integer.parseInt(nos[1]);
}
}


//generating the random number
secret = generator.nextInt((upperlimit - lowerLimit) + 1) + lowerLimit;

while (true) {
System.out.print("Enter your guess:");
guess = sc.nextInt();
if (guess < lowerLimit || guess > upperlimit) {
System.out.println("Invalid.Number Must be between " + lowerLimit + " and " + upperlimit);
continue;
} else {
/* This while loop continues to execute
* until the user guesses the number
*/
while (guess != secret) {
if (guess > secret) {
System.out.println("too high.Try again.");
no_of_guesses++;

} else if (guess < secret) {
System.out.println("too low.Try again");
no_of_guesses++;


}


System.out.print("Enter your guess:");
guess = sc.nextInt();
}

System.out.println("Correct!");
System.out.println("Bye!");

//Displaying the no of chances user took to guess
System.out.print(" No of chances you took to guess :" + no_of_guesses);
}


break;
}

}

}

__________________

Output#1:

Welcome to the Number Guessing Game
Enter the Lower Limit and upper limit :
Enter your guess:110
Invalid.Number Must be between 1 and 100
Enter your guess:50
too low.Try again
Enter your guess:70
too high.Try again.
Enter your guess:60
too high.Try again.
Enter your guess:55
too high.Try again.
Enter your guess:53
too high.Try again.
Enter your guess:52
too high.Try again.
Enter your guess:51
Correct!
Bye!

No of chances you took to guess :7

______________

Output#2:

Welcome to the Number Guessing Game
Enter the Lower Limit and upper limit :4 60
Enter your guess:70
Invalid.Number Must be between 4 and 60
Enter your guess:30
too low.Try again
Enter your guess:40
too low.Try again
Enter your guess:50
too low.Try again
Enter your guess:55
too low.Try again
Enter your guess:57
Correct!
Bye!

No of chances you took to guess :5

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote