Write a Java 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. Enter an integer number ( 0 to exit): 3241 3241 has 4 digits. Sum of digits of 3241 is 10. Enter an integer number ( 0 to exit): 264 264 has 3 digits. Sum of digits of 264 is 12. Enter an integer number ( 0 to exit): -12 -12 has 2 digits. Sum of digits of -12 is -3. Enter an integer number ( 0 to exit): 0 End. Enter a positive integer between 0 and 9: -3 Invalid input number. Enter a positive integer between 0 and 9: 15 Invalid input number. Enter a positive integer between 0 and 9: 5 0 !

Respuesta :

Answer:

Here is the JAVA program:

import java.util.Scanner;  //to accept input from user

public class Main {   //class name

   public static void   GuessTheNumber()      {  //method to guess the number

       Scanner input = new Scanner(System.in);   //creates Scanner object to accept input from user

       int number = (int)(50 * Math.random()) ;   //generates random number from 0 to 50

       int trials = 10;  //allows 10 trials to user to guess the number

       int i, userGuess;  //stores user guess

       System.out.println("Guess a number between 0 to 50");  //prompts user to guess a number between 0 to 50  

       for (i = 0; i <trials; i++) {  //allows user to keep guessing in 10 trials

           System.out.println("Enter your guess: ");   //prompts user to enter a number as a guess

           userGuess = input.nextInt();  //reads users entered number(guess)

           if (number == userGuess) {  //if users guess is correct

               System.out.println("Congratulations! The guess is correct!");  //displays congratulatory message

               break;              }  //the loop breaks after correct guess is made

           else if (number>userGuess) {  //if user guess is less than correct number

               System.out.println("Too low");             }  

           else if (number<userGuess) {  //if user guess is greater than correct number

               System.out.println("Too high");            }          }    

       if (i == trials) {  //if user has made 10 wrong guesses

           System.out.println("You exceeded 10 trials!");

           System.out.println("The correct number was " + number);          }      } //displays the correct number if user made 10 wrong guesses

 

   public static void main(String arg[])      {     //start of main method

       GuessTheNumber();      }  } //calls method to start the guessing game

Explanation:

The program prompts the user to enter a guess and it uses random() method to generate randoms numbers from 0 to 50. The loop keeps iterating and prompting the user to enter a guess until the limit of number of trials exceeds. The trials are set to 10 so the user can guess a number in 10 tries. If the user guesses a number that is higher than the correct guess then the program indicates that the guess is too high by displaying a message "too high" and if user guesses a number that is lower than the correct guess then the program indicates that the guess is too low by displaying a message "too low". If user makes 10 wrong guess then the program displays the correct guess and ends. The screenshot of the output is attached.

Ver imagen mahamnasir