Guess the Number Now we will create a number guessing game in which the user spe
ID: 3888218 • Letter: G
Question
Guess the Number
Now we will create a number guessing game in which the user specifies a range of numbers. The program will then generate a number within that range and the users guesses what it is. The program will then output the difference between the generated number and the guess.
You will need to use Java's Random class and the seed: 340L so that the numbers will become predictable for testing purposes.
The declaration and initializatoin of the Random variable will look something like this:
The Random class has a method called nextInt().
For parameters (values given in the parenthises), it can take a maximum value that is exclusive. So if you passed (put in parentheses) the number 5, the generator would give a number 0-4.
Using the user provided max and min, manipulate the Random number generator so that it gives numbers in the range [min,max] (That is the range is inclusive).
Hint: Google is your friend.
Output for this section should look like:
Explanation / Answer
Note: Run the program and check output..After that, If u want me to do any changes regarding this program ,
.Just mesage in comment section So that I will explain or modify it..Thank you.
____________________
Difference.java
import java.util.Random;
import java.util.Scanner;
public class Difference {
public static void main(String[] args) {
//Declaring variables
int randNum, userNum, min, max;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the maximum and minimum values in the range
System.out.print("Enter Minimum No in the Range :");
min = sc.nextInt();
System.out.print("Enter Maximum No in the Range :");
max = sc.nextInt();
//Creating an Random class object by passing the seed value
Random r = new Random(340 L);
//generating the random number
randNum = r.nextInt((max - min) + 1) + min;
//Getting the input entered by the user
System.out.print("Guess the number :");
userNum = sc.nextInt();
//Displaying the difference between random number and user entered number
System.out.println("The difference between the generated number and your guess:" + randNum + " - " + userNum + " = " + (randNum - userNum));
}
}
______________________
Output:
Enter Minimum No in the Range :1
Enter Maximum No in the Range :100
Guess the number :87
The difference between the generated number and your guess:48 - 87 = -39
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.