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

Develop a Java console application for a simple game of guessing at a secret fiv

ID: 3862082 • Letter: D

Question

Develop a Java console application for a simple game of guessing at a secret five-digit code (a random number from 10000 to 99999). When the user enters a guess at the code, the program outputs two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 53840 and the user guesses 83241, the digits 3 and 4 are in the correct positions. Thus, the program should respond with 2 (number of correct digits) and 7 (sum of the correct digits). Allow the user to guess until s/he gets it correct.

Here is a sample transaction:

I have randomly chosen a 5-digit code for you to guess.
Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.
For example, if the number is "68420" and you guess 12468, I will respond:
Number of Digits Correct: 1
Sum of Digits Correct : 4
From deduction, you will know the 4 was correct in the guess.

Now its your turn..................................................................

Please enter a 5-digit code (your guess):12489
Number of Digits Correct: 1
Sum of Digits Correct : 1

Please enter a 5-digit code (your guess):11358
Number of Digits Correct: 1
Sum of Digits Correct : 1

Please enter a 5-digit code (your guess):14292
Number of Digits Correct: 2
Sum of Digits Correct : 5

Please enter a 5-digit code (your guess):1450
Guess must be a 5-digit code between 10000 and 99999.

Please enter a 5-digit code (your guess):14924
Number of Digits Correct: 5
Sum of Digits Correct : 20
****HOORAY! You solved it. You are so smart****

(Extra Challenge, but not extra credit): Display how many guesses it took the user to get the code correct.

I finished a part of it please finish the project using this:

import java.util.Random;
import java.util.Scanner;

public class GuessingGame
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
int guess, correct = 0, sum = 0, attempts = 0, answer;

Random rng = new Random();
Scanner consoleScanner = new Scanner(System.in);
System.out.println("I have randomly chosen a 5-digit code for you to guess.");
System.out.println(
"Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.");
System.out.println("For example, if the number is "68420" and you guess 12468, I will respond:");
System.out.println("Number of Digits Correct: 1");
System.out.println("Sum of Digits Correct : 4");
System.out.println("From deduction, you will know the 4 was correct in the guess.");
answer = rng.nextInt(90000) + 10000;
do
{
System.out.print("Please enter a 5-digit code (your guess): ");

guess = consoleScanner.nextInt();
int g1 = guess / 100000;
int g2 = guess % 100000 / 1000;
int g3 = guess % 100000 / 100;
int g4 = guess % 100000 / 10;
int g5 = guess % 100000 / 10;
int a1 = guess / 100000;
int a2 = guess % 100000 / 1000;
int a3 = guess % 100000 / 100;
int a4 = guess % 100000 / 10;
int a5 = guess % 100000 / 10;

if (g1 == a1) {
correct++;
sum += a1;
}
}
while (guess != answer);
}

}

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class GuessingGame
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        int guess, sum = 0, attempts = 0, answer;

        Random rng = new Random();
        Scanner consoleScanner = new Scanner(System.in);
      
        System.out.println("I have randomly chosen a 5-digit code for you to guess.");
        System.out.println(
                "Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.");
        System.out.println("For example, if the number is "68420" and you guess 12468, I will respond:");
        System.out.println("Number of Digits Correct: 1");
        System.out.println("Sum of Digits Correct     : 4");
        System.out.println("From deduction, you will know the 4 was correct in the guess.");
        answer = rng.nextInt(90000) + 10000;
      
      
        do
        {
            System.out.print("Please enter a 5-digit code (your guess): ");

            guess = consoleScanner.nextInt();
            attempts++;
          
          
            //modified
            if(guess%10000==0 || guess/100000>0)
            {
                System.out.println("Guess must be a 5-digit code between 10000 and 99999.");
            }
            else
            {
                int num=answer,m=guess,correct=0,correct_sum=0;
              
                while(num>0)
                {
                    if(num%10==m%10)
                    {
                        correct++;
                        correct_sum+=m%10;
                    }
                    num=num/10;
                    m=m/10;
                  
                }
                System.out.println("Number of Digits Correct:"+correct);
                System.out.println("Sum of Digits Correct    :"+correct_sum);
              
          
            }
        }
        while (guess != answer);
      
        System.out.println("****HOORAY! You solved it. You are so smart**** The number of attempts is:"+attempts);
    }

}

output:-

run:
I have randomly chosen a 5-digit code for you to guess.
Each time you guess, I will tell you how many digits are correct and the sum of the digits that are correct.
For example, if the number is "68420" and you guess 12468, I will respond:
Number of Digits Correct: 1
Sum of Digits Correct     : 4
From deduction, you will know the 4 was correct in the guess.
Please enter a 5-digit code (your guess): 12345
Number of Digits Correct:2
Sum of Digits Correct    :7
Please enter a 5-digit code (your guess): 11341
Number of Digits Correct:3
Sum of Digits Correct    :8
Please enter a 5-digit code (your guess): 11349
Number of Digits Correct:3
Sum of Digits Correct    :8
Please enter a 5-digit code (your guess): 12349
Number of Digits Correct:2
Sum of Digits Correct    :7
Please enter a 5-digit code (your guess): 41349
Number of Digits Correct:3
Sum of Digits Correct    :8
Please enter a 5-digit code (your guess): 21349
Number of Digits Correct:3
Sum of Digits Correct    :8
Please enter a 5-digit code (your guess): 31345
Number of Digits Correct:3
Sum of Digits Correct    :8
Please enter a 5-digit code (your guess): 61349
Number of Digits Correct:4
Sum of Digits Correct    :14
Please enter a 5-digit code (your guess): 61342
Number of Digits Correct:4
Sum of Digits Correct    :14
Please enter a 5-digit code (your guess): 61343
Number of Digits Correct:5
Sum of Digits Correct    :17
****HOORAY! You solved it. You are so smart****
The number of attempts is:10
BUILD SUCCESSFUL (total time: 1 minute 59 seconds)