Task 2C: User picks an integer in a range and computer guesses by randomly This approach is similar to Task 2B. The only difference is, the computer will generate a random integer in the range as a guess. In this strategy, a guess is a random int in a given range. Hence, for the same range and same answer, the number of guesses will be different. For example, suppose the range is in [0, 10] and the answer is 7.
These are possible outputs.
The following running takes 8 tries to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10].
Computer will guess. guess #1: 10. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #2: 1. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #3: 9. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #4: 2. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #5: 4. How is my guess? 1. too big 2. too small 3. just right
guess #6: 8. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #7: 5. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #8: 7. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 3 Congratulations! The answer is 7.
In the following running, it takes only three guesses to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10].
Computer will guess. guess #1: 6. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2
guess #2: 8. How is my guess? 1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1
guess #3: The answer must be 7.
Hi,
Hope you are doing fine. I have coded the aabove question in jaava keeping all the conditions in mind. Note that, nowhere in the question it is mentioned that we must check if the user is lying, hence we assume that the user always provides valid input. The code has been clearly explained using comments that have been highligted in bold. Also find the code snippet and sample output for a clearer picture of the code.
Program:
import java.util.Random;
import java.util.Scanner;
public class randomNumber {
public static void main(String[] args) {
//creating scanner to take
input
Scanner sc=new
Scanner(System.in);
//declaring variables to
store range
int left_end,right_end;
System.out.println("Enter left end
in range: ");
//taking input of left_end
from user
left_end=sc.nextInt();
System.out.println("Enter right end
in range: ");
//taking input of right_end
from user
right_end=sc.nextInt();
//declaring variable for
guessed number, choice of user and count of guesses
int guess,choice,count=0;
//loop that breaks when
number is guessed
while(true)
{
//generating a random guess within the range of lleft_end and
right_end (both inclusive)
guess=left_end +
(int)(Math.random() * ((right_end - left_end) + 1));
//increasing count
count++;
//printing result
System.out.println("guess #"+count+": "+guess+". How is my guess?
1. too big 2. too small 3. just right");
System.out.print("Enter only 1, 2, or 3: ");
//Taking
user choice
choice=sc.nextInt();
//if the
choice is 3 then it means the number is guessed and the loop is
broken
if(choice==3)
{
System.out.println("Congratulations! The answer
is "+guess+".");
break;
}
//if the
choice is 1, it means that we must decrease the upper
range
else
if(choice==1)
{
right_end=guess-1;
}
//if the
choice is 2, it means we must increase the lower
range
else
if(choice==2)
{
left_end=guess+1;
}
//after
changing the right_end and left_end, if they are equal, it means
that the number picked by user is same as them
if(right_end==left_end)
{
count++;
System.out.println("guess #"+count+": The answer
must be "+right_end);
break;
}
}
}
}
Executable code snippet:
Sample Outputs:
Get Answers For Free
Most questions answered within 1 hours.