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

For this part you will make a program that can play a simple number guessing gam

ID: 3662435 • Letter: F

Question

For this part you will make a program that can play a simple number guessing game. You will think of a number and the computer will try to guess it, with help from you. It should start by asking for a range in which to find your number. Then, when it starts guessing, the only hints you are allowed to give are "1" (too low), "h" (too high), and "c" for a correct guess. It will repeatedly guess numbers until it finds the correct one. You should make your program guess your number in as few steps as possible. Some details: The range is inclusive, so if you say the range is "1200 to 1400" it could guess the min and max values 1200 and 1400. It should work with negative numbers If it is obvious what the answer is, it should print it and exit All of your code should go inside "GuessingGame.java" (it's in the setup folder)

Explanation / Answer

/**The java program GuessingGame that prompts user to enter
* two numbers . Then asks to type if computer guess is low or high
* or correct . The program repeats until user number is guess by the computer
* */
//GuessingGame.java
import java.util.Scanner;
public class GuessingGame
{
   public static void main(String[] args)
   {
       //Create a scanner object to read input from keyboard
       Scanner scanner=new Scanner(System.in);
      
       //declare integer variables
       int num1,num2;
       int min,max;
       //set a boolean to true
       boolean repeat=true;
      
       System.out.println("Your number is in this range...(enter two numbers)");
       //read two numbers
       num1=scanner.nextInt();
       num2=scanner.nextInt();
      
       //read new line as character
       scanner.nextLine();
      
       System.out.println("Type 'h' for too high, 'l' for too low and 'c' "
               + "if got it right ");
      
      
       //Set num1 to min and num2 to max variables
       min=num1;
       max=num2;
          
      
       /*While loop that repeates the guessing time until
       *computer guess is correct */
       while(repeat)          
       {
          
           //Find middle elements
           int guess=(min+max)/2;
          
           System.out.println("Is it "+guess);
           //Prompt for user response
           char ch=scanner.nextLine().charAt(0);
          
           if(ch=='c')
           {
               //set repeat to false to stop repeat
               repeat=false;
               System.out.println("I always win");
           }
           else if(ch=='l')
               //set guess to min if computer guess is low
               min=guess;
           else if(ch=='h')
               //set guess to max if computer guess is high
               max=guess;
          
       }//end of while
               
   }
}//end of the program

--------------------------------------------------------------------------------------------------------------------

Sample output:


Sample run1
Your number is in this range...(enter two numbers)
1200 1400
Type 'h' for too high, 'l' for too low and 'c' if got it right
Is it 1300
l
Is it 1350
h
Is it 1325
l
Is it 1337
c
I always win

sample run2

Your number is in this range...(enter two numbers)
-10 -1
Type 'h' for too high, 'l' for too low and 'c' if got it right
Is it -5
l
Is it -3
c
I always win

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