Question

In: Computer Science

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.

Solutions

Expert Solution

Below is the Java code for mentioned problem statement:

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

public class Main
{ //program will start frome here main function
   public static void main(String[] args) {
   System.out.print("Application will generate a random number between 1-20. User has to Guess the number.\nSo lets Play the game!\n");
Scanner in = new Scanner(System.in);
// Generate random number between 1 to 20
int rand_num = new Random().nextInt(20) + 1;
int count, guess;
String choice;
while (true) {
count = 0;
while (true) {
System.out.print("Enter your guess : ");
// User input
guess = in.nextInt();
count++;
if(guess == rand_num) {
break;
} else if(guess < rand_num) {
System.out.println("Your guess was low, guess again");
} else {
System.out.println("Your guess was high, guess again");
}
}
System.out.println("It took you " + count + " guesses to guess correctly");
System.out.print("DO you want to try again(yes or no): ");
choice = in.next();
if(choice.equalsIgnoreCase("no")) {
break;
}
}
}
}


Related Solutions

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...
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...
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...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money. Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program Lookup the prize...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
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 (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...
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT