Question
Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower.


Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that
Initialize a variable that represents the lowest possible number to 0 (what type should this be?)
Initialize a variable that represent the highest possible number to 100 (what type should this be?)
Initialize a Boolean variable that represents if we’ve achieved the correct guess to false
Initialize a variable in which to store the number of guesses we’ve made to 0
Declare a variable in which to store an initial guess
Declare a variable to store user input (make this an int)
Initialize a Scanner object (call the variable you assign it to “input” for consistency with class examples)
Prompt the user to choose a number (this is simply output – the program shouldn’t know the number)
Make an initial guess and store it.
Increment the value in the variable storing number of guesses.
Present guess to the user
Present prompt to user (“Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:”) Use Scanner to grab the nextInt() the user types in, and store it.
If the user has typed in a “1”
Change the value of the Boolean variable (correctGuess) to true.
While we haven’t guessed the correct number (correctGuess == false)
Modify range for next guess
Generate a next guess
Present guess to the user
Present prompt to user (“Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:”)
Use Scanner to grab the nextInt() the user types in, and store it.
If the user has typed in a “1”
Change the value of the Boolean variable (correctGuess) to true.
Print out how many steps it took to guess the number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

package abc.answers.Programs;

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        // lowest and highest possible numbers
        int low = 0, high = 100;

        // boolean variable that represents if we’ve achieved the correct guess
        boolean correctGuess = false;

        // variable to store the number of guesses we’ve made
        int countGuess = 0;

        //  variable to store an initial guess and user input
        int initialGuess, userInput;

        // Initializing a Scanner object
        Scanner input = new Scanner(System.in);
        // Prompt the user to choose a number
        System.out.println("Think a number between 0 and 100.");
    
        // declaring an array to store all possible guesses
        int[] arr = new int[101];
        // initializing the array with elements
        for(int i = 0; i <= 100; i++)
            arr[i] = i;
    
        // lower index of array
        int lower = 0;
        // upper index of array
        int upper = 100;
    
        // will execute at least once
        do {
            // find the middle index of array range between lower and upper
            int middle = (lower + (upper - lower) / 2);
            // make initialGuess as the element stored with the middle index
            initialGuess = arr[middle];
            // increment counter
            countGuess++;
            
            // present guess to the user
            System.out.println("Is " + initialGuess + " the number?");
            System.out.println("Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:");
            
            // storing user input
            userInput = input.nextInt();
            
            // choosing the correct option to proceed
            if(userInput == 1){
                correctGuess = true;
                System.out.println("Guessed correctly in " + countGuess + " tries!");
            }
            else if(userInput == 2){
                // if user number is higher then update the lower limit
                lower = middle + 1;
            }
            else if(userInput == 3){
                // if user number is lower then update the upper limit
                upper = middle - 1;
            }
        }
        // loop till the number is correctly guessed
        while(correctGuess == false);
    }
}

SCREENSHOTS:

OUTPUT:

Case 1: Guess is 100

Case 2: Guess is 0

Case 3: Guess is 48

CHANGE THE PACKAGE NAME ACCORDINGLY.

HOPE THIS HELPS!

Add a comment
Know the answer?
Add Answer to:
Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

  • Java Guessing Game Class The class will generate a random number of 1 to 15, and...

    Java Guessing Game Class The class will generate a random number of 1 to 15, and then check to see if the user guessed the number correctly. If the number is incorrect, the user should have the chance to guess again, until they guess the right number or they guess 10 times. The class method should keep track of the number of guesses the user has had, and return this value.   The class should have one constructors, a default and...

  • Computer Guesses - Create a page that allows the user to think of a number from...

    Computer Guesses - Create a page that allows the user to think of a number from 1 to 100. Write a function that will guess the number the user chose. The user will provide clues: higher or lower and correct. It should take no more than 7 guesses to get the correct number. User Guesses - Create a page that generates a random number from 1 to 100. Allow the user only 7 guesses to guess the number. Provide clues:...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT