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

Write a game program that can be played repeatedly. Using the random number gene

ID: 3642192 • Letter: W

Question

Write a game program that can be played repeatedly. Using the random number generator this game creates a five-digit number. The player guesses the number. With each guess the computer returns two numbers, the first is the number of digits that are in the proper position and the second is the sum of the correct digits. Allow the client to repeat the game as many times as she wishes. A sample run of the game would look like this:

Using the random number generator, this game creates a
five-digit number. The player guesses the number.
The computer returns two numbers, the first is the number
of digits that are in the proper position and the second
is the sum of the correct digits guessed so far.
The player continues guessing until she guesses the correct number.
11473
You have found 2 digits so far with a sum of 5.
18459
You have found 3 digits so far with a sum of 14.
16439
You have found 4 digits so far with a sum of 20.
16429
It took 4 guesses to get the number

Would you like to play the game again?
Please enter (yes/no) no

Explanation / Answer

import java.util.Scanner;

public class Guess5DNumberGame {
  
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String cont = "yes";
      
        while (cont.equals("yes")) {
            String secret = String.valueOf((int)(Math.random() * 90000) + 10000);
            String answer = keyboard.next();
            int guesses = 1;
            while (!answer.equals(secret)) {
                int count = 0;
                int sum = 0;
                for (int i = 0; i < 5; i++) {
                    if (answer.charAt(i) == secret.charAt(i)) {
                        count++;
                        sum += Integer.parseInt(answer.substring(i, i + 1));
                    }
                }
                System.out.println("You have found " + count + " digits so far with a sum of " + sum + ". ");
                guesses++;
                answer = keyboard.next();
            }
            System.out.println("It took " + guesses + " guesses to get the number");

            System.out.println("Would you like to play the game again?");
            System.out.print("Please enter (yes/no) ");
            cont = keyboard.next();
        }
    }
  
}

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