Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What is a concept test?

Answered: 1 week ago