(JAVA programming) Guess Number program

Program description: Write a program named GuessNumber that plays a game in which the program picks a secret number and the user tries to guess it.

1) The program first asks the user to enter the maximum value for the secret number.

2) Next, it chooses a random number that is >= 1 and<= the maximum number.

3) Then the user must try to guess the number.

4) When the user succeeds, the program asks the user whether or not to play another game.

The following example shows what the user will see on the screen (user input is in bold):

2. Input and output Guess the secret number.

Enter maximum value for secret number: 10

A new secret number has been chosen.

Enter guess: 3

Too low; try again. Enter guess: 8

Too low; try again. Enter guess: 9

Too low; try again. Enter guess: 10

You won in 4 guesses!

Play again? (Y/N) y

A new secret number has been chosen.

Enter guess: 7

Too high; try again.

Enter guess: 3

Too low; try again.

Enter guess: 5

You won in 3 guesses!

Play again? (Y/N) n

The user may enter any number of spaces before and after each input. The program should terminate if the user enters any input other than y or Y when asked whether to play again. Hints 1) Use two while statement (nested) for the whole program.

2) Use the following statement to pick the secret number: int secretNumber = (int) (Math.random() * maxNumber) + 1; 3) Use trim() method to trim any number of spaces in an input. 4) Use the equalsIgnoreCase method to test whether the user entered y or Y.

Respuesta :

Limosa

Answer:

The following are the program in the Java Programming Language.

import java.util.Random; //import package

import java.util.Scanner; //import package

//define a class

public class GuessNumber {

   //define a main method

   public static void main(String[] args)

   {

       //create object of the Scanner class

       Scanner cs = new Scanner(System.in);

       //print message

       System.out.println("Guess the secret number\n");

       //print message

       System.out.print("Enter the maximum value for the secret number: ");

       //get input in the integer variable through scanner class object

       int maxNum=cs.nextInt();

       //create object of the Random class

       Random ran = new Random();

       //declare an integer variable that store random number  

       int guessed = 1+ran.nextInt(maxNum);

       //set the while loop

       while (true)  

       {

           //print message

           System.out.println("\nA new secret number has been chosen.");

           //set two integer variables to 0

           int user = 0;

           int count = 0;

           //set the while loop

           while (user != guessed)  

           {

               //print message

               System.out.print("Enter guess: ");

               //get input in the variable from user

               user = cs.nextInt();

               //increment the variable by 1

               count++;

               //check that the user is less than guessed

               if (user < guessed)  

               {

                   //then, print message

                   System.out.println("low, try again.");

               }

               //check that user is greater than guessed

               else if (user > guessed)  

               {

                   //then, print message

                   System.out.println("high, try again");

               }

           }

           //print message with count

           System.out.println("You won in " + count + "!");

           //print message and get input

           System.out.print("\nPlay again? (Y/N)");

           cs.nextLine();

           String again = cs.nextLine();

           //check that input is y

           if(again.equalsIgnoreCase("y"))

           {

               //then, loop again iterates

               continue;

           }

           //otherwise, loop is break

           else{

               break;

           }

       }

   }

}

Explanation:

The following are the description of the program :

  • Firstly, we import the required packages and define the class 'GuessNumber'.
  • Inside the class, we create the object of the scanner class and the random class then, get input from the user through scanner class object and generate random number through the random class object.
  • Then, set the while infinite loop in which we declare two integer data type variables and assign them to 0 then, set while loop inside the infinite loop that iterates when the 1st variable is not equal to the second one then, get input from the user through scanner class object.
  • Then, check that input number is less than the random number then, print message otherwise, again check that input number is greater than the random number then, print message or if both numbers are equal then print the message with the count.
  • Finally, the program asks for play again, if the input is 'y' then, infinite loop again iterates otherwise the infinite loop is break.

The java program to compile the numbers gotten from the user request is:

  1. import java.util.Scanner; //import package
  2. public class GuessNumber {
  3.   public static void main(String[] args)
  4.   {
  5.       Scanner cs = new Scanner(System.in);
  6.       System.out.println("Guess the secret number\n");
  7.       System.out.print("Enter the maximum value for the secret number: ");
  8.       int maxNum=cs.nextInt();
  9.       Random ran = new Random();
  10.       //declare an integer variable that store random number  
  11.       int guessed = 1+ran.nextInt(maxNum);
  12.       //set the while loop
  13.       while (true)  
  14.       {
  15.           //print message
  16.           System.out.println("\nA new secret number has been chosen.");
  17.           //set two integer variables to 0
  18.           int user = 0;
  19.           int count = 0;
  20.           //set the while loop
  21.           while (user != guessed)  
  22.           {
  23.               //print message
  24.               System.out.print("Enter guess: ");
  25.               //get input in the variable from user
  26.               user = cs.nextInt();
  27.               //increment the variable by 1
  28.               count++;
  29.               //check that the user is less than guessed
  30.               if (user < guessed)  
  31.               {
  32.                   //then, print message
  33.                   System.out.println("low, try again.");
  34.               }
  35.               //check that user is greater than guessed
  36.               else if (user > guessed)  
  37.               {
  38.                   //then, print message
  39.                   System.out.println("high, try again");
  40.               }
  41.           }
  42.           //print message with coun
  43.           System.out.println("You won in " + count + "!");
  44.           //print message and get input
  45.           System.out.print("\nPlay again? (Y/N)");
  46.           cs.nextLine();
  47.           String again = cs.nextLine();
  48.           //check that input is y
  49.           if(again.equalsIgnoreCase("y"))
  50.           {
  51.               //then, loop again iterates
  52.               continue;
  53.           }
  54.           //otherwise, loop is break
  55.           else{
  56.               break;
  57.           }
  58.       }
  59.   }
  60. }

Read more about java programming here:

https://brainly.com/question/18554491

ACCESS MORE