In: Computer Science
in java
Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with the target number.
CODE:
import java.util.concurrent.ThreadLocalRandom;
import java.util.*;
public class rand
{
public static int getRandomValue(int Min, int Max)
{
return ThreadLocalRandom .current() .nextInt(Min, Max + 1);
}
public static void main(String[] args)
{
Scanner ob=new Scanner(System.in);
System.out.println("enter min number:");
int Min=ob.nextInt();
System.out.println("enter max number:");
int Max=ob.nextInt();
System.out.println("enter guess number:");
int guess=ob.nextInt();
int xxx= getRandomValue(Min, Max);
if(guess==xxx)
{
System.out.println("your guess is correct");
}
else
{
System.out.println("your guess is incorrect , i was looking for
"+xxx);
}
}
}
REFERENCE:
OUTPUT: