Question

In: Computer Science

guessing game in Java. It will have a guess input used for guessing the random number...

guessing game in Java.

It will have a guess input used for guessing the random number that is generated from 1 - 100.

When the user makes a guess it will tell them if the number is lower or higher than the guess.

There is also a choice to give up which then reveals the correct number.

The last choice will be new game which resets the random number.

Last, the program should keep counts of tries.

When the user guesses the correct answer, the program will say “You got in in 5 tries” if the user took guesses to get it.

The number of tries should reset to 0 for each new game.

Add a simple dialog that tells the user when he guesses correctly.

Solutions

Expert Solution

CODE
import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Random rand = new Random();
      
       Scanner scan = new Scanner(System.in);
       int newgame = 1;
       while(newgame==1){
       int number = rand.nextInt(100);
       System.out.println("Lets start new game");
       int guess = -1;
       int attempts = 0;
       //we put a while loop here because program will continue till correct answer is not guessed
       while(guess!=number){
       System.out.println("Please enter your guess");
       guess = scan.nextInt();
       if(guess<number){
       System.out.println("Your guess is less than the number");
       attempts++;
       }
       else if(guess>number){
       System.out.println("Your guess is greater than the number");
       attempts++;
       }
       }
       if(guess==number){
       System.out.println("Congratulations!! You got in "+ attempts+" tries");
       System.out.println("Do you wanna play again?\n Yes, then press 1\n No, then press 0");
       //while loop will terminate if no is choosen, as while continues till newgame==1;
       newgame = scan.nextInt();
       }
      
      
       }
      
   }
  
}

ALGORITHM
1. First import java.util.*;
   It will be needed for Scanner class and Random class.
2. We make a while loop that will not terminate till you keep on asking to play new game.
   number variable contains random number;
3. Inside this we again create a while loop which will not terminate till you have guessed correct.
   guess variable represents your guess;
   attempts counts your wrong tries;
4. After the correct guess there is dialog that if player wants to play a new game.

I am attaching screenshot of output with my answer.


Related Solutions

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 Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
using xsd, Create a number guessing game You would need an input box and buttons Ask...
using xsd, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison ​a. Make sure to provide hints back to the user if the number enter is low or high ​​-this element should be automatically added ​b. Clear the value enter in the text ​c. Give them 3 tries Whether the user wins or loses, display all the guesses along with the...
using xml, Create a number guessing game You would need an input box and buttons Ask...
using xml, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison             a. Make sure to provide hints back to the user if the number enter is low or high                         -this element should be automatically added             b. Clear the value enter in the text             c. Give them 3 tries Whether the user wins or loses, display all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT