Question

In: Computer Science

In JAVA, write a number guessing game that randomly assigns a integer between one and twenty...

In JAVA, write a number guessing game that randomly assigns a integer between one and twenty for the user to guess.

Write three methods to make this particular program work.

1. Have a method which generates a random number and returns the value to the main (stored as a variable)

2. Have a method which prompts the user for a guess. Return the guess to the main.

3. Evaluate the guess and the random "secret" number by parameters, and then have it evaluate if the guess is correct. RETURN A STRING to let the user know if the guess was correct, too high, or too low.

Use a nested loop in main. Ask the user if they would like to play again. Use an inner loop that continually calls the third method until the guess is correct.

Solutions

Expert Solution

Code:-

import java.lang.*;
import java.util.*;
class game{
   public static int generator(){
       Random rand = new Random(); //random number generator
       return rand.nextInt(20); //returning random number
   }
   public static int userguess(){ //function to take input from user
       System.out.print("Enter your Guess: ");
       Scanner scan=new Scanner(System.in); //scanner
       int guess=scan.nextInt(); //taking input from user
        return guess; //returning guess
   }
   public static String evaluate(int rand,int guess){ //function to return string
       if(guess>rand) //if guess is higher than random number
           return "TOO HIGH";
       else if(guess<rand) //if guess is lower than random number
           return "TOO LOW";
       else //if guess and random number are equal
           return "CORRECT";
   }
   public static void main(String[] args) { //main function
       while(true){ //loop until use enter want to exit by clicking n
           int rand=generator(); //calling function
           while(true){ //loop until user guess the correct number
               int guess=userguess();
               String eval=evaluate(rand,guess); //calling function
               System.out.println(eval); //calling function
               if(eval.equals("CORRECT")) //if guess is correct
                   break;
           }
           System.out.print("Do You Want to play Again(y/n): "); //enter n for stop
           Scanner scan=new Scanner(System.in);
           char ch=scan.nextLine().charAt(0); //taking input from user
           if(ch=='n') //if user enters n
               break; //breaking the loop
       }
   }
}

Output:-


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 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...
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...
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...
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...
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...
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10....
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10. Have the user guess the number. If it is too high, tell the user to guess lower - it it is too low, tell the user to guess higher. Continue until she guesses the correct number. - Part 2. Allow the user to play a new game after they have guessed correctly.   Part 3. Ask for a different name and keep track of how...
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...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT