Question

Goal: Write a simple number guessing game in java. The game picks a number between bounds...

Goal: Write a simple number guessing game in java.

  1. The game picks a number between bounds input by the user, then user has to guess the number.

  2. The game will tell you if your guess is too high or too low.

  3. When you guess it, the game will tell you how many guesses it took

  4. Run multiple games and print statistics (# games, # guesses, avg) when all done.

Sample Run:

G U E S S I N G G A M E !

Enter lower and upper bounds:

1

10

Enter a number from 1 to 10 (inclusive):

5

Correct, the number was 5! You got it in 1 tries.

Again? (yes or no):

yes

Enter a number from 1 to 10 (inclusive):

5

Too high...

Enter a number from 1 to 10 (inclusive):

3

Correct, the number was 3! You got it in 2 tries.

Again? (yes or no):

no

You played 2 games and made 3 guesses total (1.5 guesses per game).

T H A N K S F O R P L A Y I N G !

Homework Answers

Answer #1

Source Code:

import java.util.Scanner;
import java.util.Random;
class ex{
   public static void main(String[] args) {
       Scanner scnr=new Scanner(System.in);
       Random rand=new Random();
       System.out.println("G U E S S I N G G A M E ! ");
       System.out.println("Enter lower and upper bounds:");
       int min=scnr.nextInt();
       int max=scnr.nextInt();
       float games=0,guesses=0;
       String choice="yes";
       while(choice.equals("yes")){
           int userGuess=0;
           int tries=1;
           int rand_int=rand.nextInt((max-min+1))+min;
           while(userGuess!=rand_int){
               System.out.println("Enter a number from "+min+" to "+max+" (inclusive):");
               userGuess=scnr.nextInt();
               ++tries;
               guesses=guesses+1;
               if(userGuess==rand_int){
                   System.out.println("Correct,the number was "+rand_int+"!you got it in "+tries+" tries.");
                   break;
               }
               else if(userGuess<rand_int)
                   System.out.println("Too low...");
               else if(userGuess>rand_int)
                   System.out.println("Too high...");
           }
           System.out.println("Again?(yes or no):");
           choice=scnr.next();
           games=games+1;
       }
       System.out.println("You played "+games+" games and made "+guesses+" guesses total("+(guesses/games)+" guesses for game).");
       System.out.println("T H A N K S F O R P L A Y I N G !");
   }
}

Sample input and output:

Know the answer?
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
Similar Questions
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a program that plays a number guessing game with a human user. The human user...
Write a program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Create a very simple TCP Application in JAVA. You will create a client and a server....
Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do. The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Draw a flowchart and pseudocode that describes the process of guessing a number between 1 and...
Draw a flowchart and pseudocode that describes the process of guessing a number between 1 and 100. -Make a working version of this program in Python. ( need the correct code for program ) -After each guess, the player is told that the guess is too high or too low. -The process continues until the player guesses the correct number.
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
Write a script that plays a simple “guess the number” game with the user. It should...
Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Alice and Barbara are playing a one-stage guessing game. Each must choose a number between 1...
Alice and Barbara are playing a one-stage guessing game. Each must choose a number between 1 and 4 (inclusive). Alice’s target is to match Barbara’s number. Barbara’s target is to name twice Alice’s number. Each receives $10 minus a dollar penalty that is equal to the absolute difference between her guess and her target. Solve this game by iteratively deleting dominating strategies. What will Alice and Barbara choose?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT