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
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...
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?
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write...
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write a program where the user and the program/computer play a number guessing game. The program should prompt the user for a number (between 1 and 100, inclusive) then the program/computer has to guess what the user entered. Keep track of the number of iterations it takes for the computer to guess the number. Sample runs are shown below: Enter number to be guessed: 88...
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0...
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0 to 10 '); if(R>guess) disp('Your guess is too small') elseif (R<guess) disp('Your guess is too large') elseif (R==guess) disp('You are correct! It is ');guess R=floor( rand()*10 ); end count=count+1; if(count==3) break; end end You need to modify this code: (1)Player fails the game if you cannot make it within 3 tries. (2)Player can do this game up to 10 times. (3)If player input is...
Here is my problem: In Swift I, you used your knowledge at the time to create...
Here is my problem: In Swift I, you used your knowledge at the time to create a High / Low Guessing Game.   This week, you will update (or create a new version of) the game using the knowledge you've now attained. You will be creating a High/Low guessing game as outlined below: In this game: Your app will think of a random number between 0 and 999, and you (the player) will have to guess the number. You can use...
This is a very short trivia game. See if the user can guess the year that...
This is a very short trivia game. See if the user can guess the year that MDC (formerly Dade County Junior College) first started offering classes (1960). If the user guesses incorrectly, ask if they want another attempt. Once the user gets it right, report the number of guesses needed and end the program. Initial Output: MDC was originally named Dade County Junior College. Output (per loop) In what year were classes first offered? [user types: 1980] That answer is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT