I need help with this assignment Please include comments thr

I need help with this assignment. Please include comments throughout the program. Thanks

Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns 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 13720, and the user guesses 83521, the digits 3 and 2 are in the correct position. Thus, the program should respond with 2 and 5. Allow the user to guess only 10 times.

Solution

Hi, Please find my code.

Please let me know in case of any issue.

import java.util.Scanner;

public class GussingGame {

  

   public static void main(String[] args) {

      

       // creating scanner object

       Scanner sc = new Scanner(System.in);

      

       // secret code as character array

       char[] secret = {\'1\', \'3\', \'7\', \'2\', \'0\'};

      

       int count = 0;

       String usetGuess;

      

       int numberOfMatch, sum;

      

       while(count < 10){

          

           // initializing sum and numberOfMatch

           sum = 0;

           numberOfMatch = 0;

          

           // taking user guess

           System.out.print(\"Enter your 5 digit guess: \");

           usetGuess = sc.next();

          

           // converting user guess in char array

           char userGuessArray[] = usetGuess.toCharArray();

          

           // check for number of match and count match and sum

           int i=0;

           while(i < 5){

               if(secret[i] == userGuessArray[i]){

                   numberOfMatch++;

                   sum = sum + (secret[i] - \'0\');

               }

               i++;

           }

          

           if(numberOfMatch == 5){// matched all digits

               System.out.println(\"You Got it!!!\");

               break; // stop

           }else

               System.out.println(\"Number of matched digit = \"+numberOfMatch+\", sum = \"+sum);

          

           count++;

       }

   }

}

/*

Sample run:

Enter your 5 digit guess: 12345

Number of matched digit = 1, sum = 1

Enter your 5 digit guess: 15678

Number of matched digit = 1, sum = 1

Enter your 5 digit guess: 13543

Number of matched digit = 2, sum = 4

Enter your 5 digit guess: 13760

Number of matched digit = 4, sum = 11

Enter your 5 digit guess: 13720

You Got it!!!

*/

I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of
I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of
I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site