Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA programming) Guess Number program Program description: Write a program nam

ID: 3683355 • Letter: #

Question

(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.

Explanation / Answer

Java Program to create a Guess Number Game

GuessNumber.java

package org.students;

import java.util.Scanner;

public class GuessNumber {
   public static void main(String[] args) {
       int sectNum;
       String guesstr,maxstr;
       int guessNo;
       int maxSecNum;
       //This class is used Read the input from keyboard
       Scanner sc = new Scanner(System.in);
       while(true)
       {
           //To know in how many guesses user guessed the Secret Number.
           int count=0;
           //Print Statement
           System.out.print("Enter maximum value for secret number: ");
           //This will read the string as input from key board
           maxstr = sc.next();
           //Cut the spaces and convert into Integer value
           maxSecNum=Integer.parseInt(maxstr.trim());
           //To generate the Random Number.
           sectNum = (int) (Math.random() * maxSecNum + 1);
           System.out.println("** A new secret number has been chosen **");
          
           // this while loop will executes until user guessed the number correctly.If guessed correctly I will prompt to play again (or) not.
           while(true){
               count++;
               System.out.print("Enter Guess: ");
               guesstr = sc.next();
               guessNo=Integer.parseInt(guesstr.trim());
              
               //check if secret number is eual to guessed number or not.
               if (guessNo == sectNum)
               {
                   System.out.println("You won in "+count+" guesses");
                   System.out.print("Play Again ?(Y/N):");
                   String c =sc.next();
                   if(c.equalsIgnoreCase("y"))
                   {
                       break;
                   }
                   //Program will terminate if the user didnt like to play again by pressing other than 'y' or 'Y'
                   else
                   {
                       System.out.println("* Program Exit *");
                       System.exit(0);
                      
                   }
                      
               }//These messages will guide user in guessing secret number
               else if (guessNo < sectNum)
                   System.out.println("* Too low; try again *");
               else if (guessNo > sectNum)
                   System.out   .println("* Too high; try again *");
              
           }
       }
      
      
   }

}

____________________________________________________________________________________________

output:

Enter maximum value for secret number: 10
** A new secret number has been chosen **
Enter Guess: 2
* Too low; try again *
Enter Guess: 5
* Too low; try again *
Enter Guess: 6
* Too low; try again *
Enter Guess: 7
* Too low; try again *
Enter Guess: 8
You won in 5 guesses
Play Again ?(Y/N):y
Enter maximum value for secret number: 4
** A new secret number has been chosen **
Enter Guess: 3
* Too low; try again *
Enter Guess: 4
You won in 2 guesses
Play Again ?(Y/N):n
* Program Exit *

_____________________________________________________________________________________________