Question

In Java You’re going to make a Guess the Number game. The computer will "think" of...

In Java

You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries.

The program should look like this in the console, player input is in bold:

Hello! What is your name?

Abaddon

Well, Abaddon, I am thinking of a number between 1 and 20.

Take a guess.

10

Your guess is too high.

Take a guess.

2

Your guess is too low.

Take a guess.

4

Good job, Abaddon! You guessed my number in 3 guesses!

Would you like to play again? (y or n)

n

Process finished with exit code 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//GuessingGame20.java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame20 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Hello! What is your name?");
        String name = sc.nextLine();
        boolean keepPlaying = true;

        Random rand = new Random();
        int randValue = 1+rand.nextInt(20);
        int count;
        while(keepPlaying) {
            count = 1;
            System.out.println("Well, "+name+", I am thinking of a number between 1 and 20");
            System.out.println("Take a guess.");
            int userNum = sc.nextInt();
            while (userNum != randValue) {
                count++;
                if (userNum > randValue) {
                    System.out.println("Your guess is too high.");
                } else {
                    System.out.println("Your guess is too low.");
                }
                System.out.println("Take a guess.");
                userNum = sc.nextInt();
            }
            System.out.println("Good job, "+name+"! You guessed my number in " + count + " guesses!");

            System.out.println("Would you like to play again? (y or n)");
            String playResponse = sc.next();
            keepPlaying = !(playResponse.startsWith("N") || playResponse.startsWith("n"));
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
In Java You’re going to make a Guess the Number game. The computer will "think" of...
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
  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • 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...

  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • 7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...

    7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number” as follows: Your app chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The app displays the prompt "Guess a number between 1 and 1000: ". The player inputs a first guess. If the player’s guess is incorrect, your app should display Too high. Try again. or Too low. Try again. to help the player “zero...

  • Create a JavaFX game: Guess the Number create a random # between 1 and 1000 Ask...

    Create a JavaFX game: Guess the Number create a random # between 1 and 1000 Ask user for a guess; possible answers TOO LOW TOO HIGH WINNER! print guess to screen if the user wins, write the random number, and all the guesses to a file. If the user doesn't guess in 10 turns, display the number. NOTES: You may want to implement: Restart option Best Guess statistic (game 1 took 8 tries, game 2 took 5 - 5 is...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing...

    USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing game. For playing "Guess a Number", the program will generate a number for player to guess from within a range specified by the player. The program will take as input the lowest number in the range and the highest number in the range from the player, and then a series of guesses of the form: <n,>n, = n, where n is the number guessed....

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
ADVERTISEMENT