Question

Write a program(JAVATPOINT) that generates a random number between 0 and 100 and asks the user...

Write a program(JAVATPOINT) that generates a random number between 0 and 100 and asks the user to guess it. The user can have at maximum 10 trials. If the number is guessed, the user should be asked if she/he wants to play again. If the number is not guessed and 10 trials were used, the user is not lucky, the program should terminate with a proper message.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

import java.util.*;
class NumberGuess{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Random rand = new Random();
       int secret = rand.nextInt(100);
       int numOfGuesses = 10;
       int noOfWins = 0;
       while (true) {
           int guesses = 0;
           boolean isGuessed = false;
           while (guesses <= numOfGuesses) {
               System.out.print("Enter number between 0 to 100: ");
               int num = sc.nextInt();
               guesses++;
               if(num == secret){
                   isGuessed = true;
                   noOfWins++;
                   System.out.println("You guessed it in "+guesses+" tries.");
                   break;
               }else if(num > secret){
                   System.out.println("Enter low number");
               }else{
                   System.out.println("Enter high number");
               }
           }
           sc.nextLine();
           if(!isGuessed){
               System.out.println("Better luck next time.");
           }
           System.out.print("\nDo you want to play again? (Y/N): ");
           String s = sc.nextLine();
           if(s.toLowerCase().equals("n"))
               break;
           secret = rand.nextInt(100);
       }
       System.out.println("\nYou won in "+noOfWins+" games");
   }
}

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

        THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
Write a program(JAVATPOINT) that generates a random number between 0 and 100 and asks the user...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program 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. Be sure that your program...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number 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.

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • Exercise 1 Write a program that asks the user to enter the seed for a random...

    Exercise 1 Write a program that asks the user to enter the seed for a random number generator. Then the program uses this seed to roll a dice and hence generates two integers corresponding to the faces of each dice. If their sum is odd, the user has two tries to guess the sum. The program gives the user a hint after the first try. Otherwise, the user has three tries to guess the sum. Sample run 1: Enter the...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Write a C program that generates a random number between 0 and 50. Then, the program...

    Write a C program that generates a random number between 0 and 50. Then, the program prompts for guesses until the user is correct, or has made 10 wrong guesses. After each guess, the program indicates whether the guess was too high, too low, or correct. Once the round has ended, either by a correct guess or by using up the 10 guesses, the program displays the current status.

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Write a C++ program that generates a random number from 0 to 9. It will then...

    Write a C++ program that generates a random number from 0 to 9. It will then prompt the user to guess the random number it generated. Your program stops if the user guesses the right number. (Use the cout function to display the random number the computer generates so that you can determine if your code actually works). SAMPLE OUTPUT Random number = 8 Guess a number in the range [0,9]: 1 Too low Guess a number in the range...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT