Question

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 it.
    • Else, tell the user to try again
  • You should keep track of how many times they tried guessing and then display the number of times it took them to guess it if they did.
  • If they exceed the number of allowed trials, display a message telling them that and display the number that you created.
  • Display the remaining number of trials every time they guess.
  • Tell the user if they are above or below the number.
  • If they are close to guessing the number (about 2 numbers off) you should tell the user that they are very close to guessing the number.

Homework Answers

Answer #1

Source Code:

import java.util.Scanner;
import java.util.Random;

class ex
{
   public static void main(String[] args) {
       Scanner input=new Scanner(System.in);
       Random rand=new Random();
       int min=1,max=20;
       int count=10,tries=0;
       System.out.println("Enter a number to guess the number between 1 and 20");
       int rand_num=(rand.nextInt(max)) + min ;
       int guess=0;
       while(count>0)
       {
           ++tries;
           --count;
           guess=input.nextInt();
           if(guess==rand_num)
               break;
           else if(rand_num-guess ==2 || rand_num-guess == -2 || rand_num-guess ==1 || rand_num-guess == -1)
               System.out.println("Incorrect, you are very close to number, you have "+count+" trials remaining");
           else if(guess>rand_num)
               System.out.println("Incorrect, your guess is high and you have "+count+" trials remaining");
           else
               System.out.println("Incorrect, your guess is low and you have "+count+" trials remaining");
       }
       if(count==0)
           System.out.println("You reached maximum trials and the number is "+rand_num);
       else
           System.out.println("You guessed correctly in "+tries+" tries. The number is "+rand_num);
   }
}

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
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. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
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.
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration)....
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...
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...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
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 FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...