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

  • 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...

  • 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...

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

  • 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...

  • 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...

  • 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...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • 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....

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

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
ADVERTISEMENT