Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!!!

*/

Add a comment
Know the answer?
Add Answer to:
I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer...

    c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer picks a secret number: the secret number must be 3 digits, none of the digits can be the same, and it cannot start with a zero. 104 is OK, but 091 and 212 are not. The user tries to guess the secret number - if none of the digits in the user's number are in the secret number, then the program replies with BAGEL...

  • JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game...

    JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game is started, generate a random letter between A and Z; 2). Display a message "I have a secret letter (A to Z), can you guess what it is?"; 3). Read the user's answer; 4). Compare the user's answer and the random secret letter generated; 5). If the user answer is before the random secret letter in the alphabet, display "Incorrect. Try something bigger" and...

  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • How can I do this program complete with input validation so that the computer prompts the...

    How can I do this program complete with input validation so that the computer prompts the user to enter a number 0 through 100 if he/she guesses anything lower than 0, a floating point number, or other characters? And later, prompts the user to enter yes or no if he/she enters anything else when the program prompts the user to play again? Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number...

  • Code in JAVA.................... Guess the Number In this activity, you will write a REST server to...

    Code in JAVA.................... Guess the Number In this activity, you will write a REST server to facilitate playing a number guessing game known as "Bulls and Cows". In each game, a 4-digit number is generated where every digit is different. For each round, the user guesses a number and is told the exact and partial digit matches. An exact match occurs when the user guesses the correct digit in the correct position. A partial match occurs when the user guesses...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • Hello, Could you please explain how I would complete this program with input validation to ensure...

    Hello, Could you please explain how I would complete this program with input validation to ensure that an error message will not appear if the user enters something other than an integer that is not 1-100 and later if they enter anything other than yes and no? Here is the program: Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly promt the user to...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads...

    With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads a text file “input.txt” and stores all the distinct words in an array. A word consists of letters only - uppercase and/or lowercase. An incoming word should be inserted into the array such that it is always in ascending order. Use binary search to ensure that no duplicate words are added. Assume that there are no more than 100 distinct words. Assume that the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT