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

Write an interactive Java program. Write a number guessing game program. Ask the

ID: 3686230 • Letter: W

Question

Write an interactive Java program. Write a number guessing game program. Ask the player how many games they would like to play. Then ask the player to pick a difficulty level for the games. Easy:1-5, Normal: 1-10, Hard: 1-100. Prompt the user to start guessing numbers, giving them hints as to whether they are to hi or low along the way. Keep up with the total number of guesses taken for a match summary after all games complete. It should look something like this:

How many games would you like to play: 2
Would you like the games to be easy, normal, or hard: normal
Guess a number between 1-10: 5
To high. Guess again: 3
Correct!
Guess a number between 1-10: 5
To low. Guess again: 7
To high. Guess again: 6
Correct!
It took you 5 guess to win 2 games, Congratulations!
It took you an average of 2.5 guesses per game!

Explanation / Answer

Program:

import java.util.Scanner;
import java.util.Random;
public class Testing2 {
   public static void main(String str[]){
       int numPalyers=0;
       int number=0;
       int guessNumber=0;
       Random randomGenerator = new Random();
       Scanner sca=new Scanner(System.in);
       System.out.println("how many games they would like to play: ");
       numPalyers=sca.nextInt();
       System.out.println("Would you like the games to be easy, normal, or hard:");
       String level=sca.next();
       int []count=new int[numPalyers];
       for(int i=1;i<=numPalyers;i++){
           count[i-1]=0;
           if(level.equals("easy")){
               System.out.println("Guess a number between 1-5: ");
               number = randomGenerator.nextInt(5)+1;
           }else if(level.equals("normal")){
               System.out.println("Guess a number between 1-10: ");
               number = randomGenerator.nextInt(10)+1;
           }else if(level.equals("hard")){
               System.out.println("Guess a number between 1-100: ");
               number = randomGenerator.nextInt(100)+1;
           }
          
           guessNumber=sca.nextInt();
           count[i-1]++;
           while(guessNumber!=number){
               if(guessNumber>number){
                   System.out.println("To high. Guess again: ");
               }else{
                   System.out.println("To low. Guess again: ");
               }
               guessNumber=sca.nextInt();
               count[i-1]++;
       }
           System.out.println("Correct!");
       }
       int totalcount=0;
       for(int i=0;i<numPalyers;i++){
           totalcount+=count[i];
       }
       System.out.println("It took you "+totalcount+" guess to win "+numPalyers+" games, Congratulations!");
       System.out.println("It took you an average of "+(1.0*totalcount/numPalyers)+" guesses per game!");
   }
}

Result:

how many games they would like to play:
2
Would you like the games to be easy, normal, or hard:
normal
Guess a number between 1-10:
5
Correct!
Guess a number between 1-10:
7
To low. Guess again:
9
To low. Guess again:
10
Correct!
It took you 4 guess to win 2 games, Congratulations!
It took you an average of 2.0 guesses per game!

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