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

Write a program to play a guessing game that picks a random number between 1 and

ID: 3674863 • Letter: W

Question

Write a program to play a guessing game that picks a random number between 1 and 100. Ask the player to guess the number. On each incorrect attempt, give the player a hint of "higher" or "lower" and tell them how many tries they have left. If they guess the number within 7 attempts, they win, otherwise, they lose. Report the outcome and ask the player if they would like to play again.

Random numbers within the range can be generated using the Math.random() method as follows:
(int)((Math.random() *100) + 1)

Explanation / Answer

import java.util.Scanner;

public class HelloWorld {
   public static Scanner input = new Scanner(System.in);

   public static void main(String[] args) {
      
       guessingGame();
   }
      

  

   /**
   * Number of guesses user have until game is over.
   * <p>
   * Suggested number for normal game is 6, for easy mode is 8.
   *
   * @return Number of guesses user have at disposal.
   */
   public static int gameMode() {
       return 6;
   }

   /**
   * Creates random number from (and including) 1 to 100.
   *
   * @return <code>int</code> type random number
   */
   public static int getRandomNumber() {
       return (int) (Math.random() * 100) + 1;
   }

   /**
   * If user selects to play again send answer to playGame method to play
   * again.
   *
   * @return <code>Boolean</code> type value
   */
   public static boolean playAgain() {
       //Scanner input = new Scanner(System.in);
       System.out.println("If you want to play again enter "yes".");
       System.out.println("If you want to exit simply press enter.");

       String again;
       again = input.nextLine();
       if (again == ("yes")) {
           System.out.println(again);
           return true;
       }else{
           return false;
      
      
   }
}
   /**
   * Prints welcome message with some instruction to standard output.
   */
   public static void welcomeMessage() {
       System.out.println("Welcome to guessing game!");
       System.out
               .print("Computer will pick a number between 1 and 100, your goal is to guess it! ");
       System.out.println("Good luck, and have fun!");
       System.out.println();
   }

   /**
   * Message printed only at end of the game.
   */
   public static void outroMessage() {
       System.out.println("Thank you for playing!");
       System.out.println("Bye bye!");

   }

   /**
   * Game of guessing a randomly selected number.
   * <p>
   * For every user input computer tells him/her that guess is low or high. If
   * user guess the number before gameMod limitation he/she wins.
   * <p>
   * gameMod limitation is actually a number of guesses user have at disposal.
   * For changing from harder to easier game go to gameMod method.
   *
   */
   public static void playGame() {

       int guessCount = 0;
       int userGuess;
       int randomNum = getRandomNumber();

       while (true) {
           System.out.println("Try to guess my number!");
           userGuess = input.nextInt();
           guessCount++;
           // System.out.println(randomNum);
           if (userGuess == randomNum) {
               System.out
                       .printf("Your got it! It only took you %d attempt(s) to guess it.",
                               guessCount);
               playAgain();
               break;
           }
           if (userGuess > randomNum) {
               System.out.println("Your number is higher! Try again");
           } else if (userGuess < randomNum) {
               System.out.println("Your number is lower! Try again");
           }
           if (guessCount == gameMode()) {
               System.out.printf(
                       "Game over! You must guess in %d attempts! ",
                       gameMode());
               playAgain();

       break;

           }
       }
   }

   /**
   * Entire game of guessing number. User can choose to play again or to end
   * after each game played.
   */
   public static void guessingGame() {

       boolean again ;

       while (again = true) {
           welcomeMessage();
           playGame();
           again = playAgain();
          
  
      
       }
       outroMessage();
   }      

}


output

Welcome to guessing game!                                                                                                                                   
Computer will pick a number between 1 and 100,                                                                                                              
your goal is to guess it!                                                                                                                                   
Good luck, and have fun!                                                                                                                                    
                                                                                                                                                            
Try to guess my number!                                                                                                                                     
4                                                                                                                                                           
Your number is lower! Try again                                                                                                                             
Try to guess my number!                                                                                                                                     
5                                                                                                                                                           
Your number is lower! Try again                                                                                                                             
Try to guess my number!                                                                                                                                     
8                                                                                                                                                           
Your number is lower! Try again                                                                                                                             
Try to guess my number!                                                                                                                                     
9                                                                                                                                                           
Your number is lower! Try again                                                                                                                             
Try to guess my number!                                                                                                                                     
10                                                                                                                                                          
Your number is lower! Try again                                                                                                                             
Try to guess my number!                                                                                                                                     
11                                                                                                                                                          
Your number is lower! Try again                                                                                                                             
Game over! You must guess in 6 attempts!                                                                                                                    
If you want to play again enter "yes".                                                                                                                      
If you want to exit simply press enter.                                                                                                                     
If you want to play again enter "yes".                                                                                                                      
If you want to exit simply press enter.                                                                                                                     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote