Question: help help this is my code soo far for some reason i type the digit correct that with other random digits and still get zero

help help this is my code soo far for some reason ihelp

help this is my code soo far for some reason i type the digit correct that with other random digits and still get zero this is my code so far

-----------

import java.util.Random;

import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {

// create scanner

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.");

System.out.println();

System.out.println("Now its your turn..................................................................");

//generate the secret code

Random rand = new Random( );

int secretCode = 00000;

secretCode = rand.nextInt(9000) + 10000;

int Maxattempts = 0;

//initialize variable

int userDigit;

int attempts = 0, secretDigit;

//execute body if while loop if attempts

// does not exceed maximum attempts

while (attempts == Maxattempts)

{

System.out.print("Please enter a 5-digit code (10000-99999): ");

//read the user guess.

int userGuess = consoleScanner.nextInt();

//copy the same thing of user guess but this time its userguess1

int userGuess1 = userGuess;

//copy secret code to secretcode1

int secretCode1 = secretCode;

//initialize variable

int correctPositions = 0, sum = 0;

//compare each digit of user guess

for (int i = 0; i

{

//get the last digit

userDigit = userGuess1 % 10;

secretDigit = secretCode1 % 10;

// compare

if(secretDigit == userDigit)

{

correctPositions++;

sum += userDigit;

}

//remove the last digit

userGuess1 = userGuess1 / 10;

secretCode1 = secretCode1 / 10;

}

// if the user is correct..

if(correctPositions == 5)

{

// if user got the answer right.

System.out.println("****HOORAY! You solved it. You are so smart****");

System.out.println("Number of digits correct: " + correctPositions);

System.out.println("sum of Digits Correct: " + sum);

// get out of the while loop

consoleScanner.close();

System.exit(0);

}

//user hasn't win yet. display the result of current

else

System.out.println("number of digits correct = " + correctPositions);

System.out.println("sum of Digits Correct: " + sum);

}

}

}

Safari File Edit View History Bookmarks Develop Window Help DAT 85% Sat 6:53 PM occ.blackboard.com Madeon x Porter Robinso n x VDG Come Undon. Week 6: 3/5 3/11 32865.20163 Home I Chegg.com Google Java Project Name: IC11 GuessingGame 99999 000 00 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 conrect position and the sum of those digits. For example, if the secret code is53840 and the use guesses 8324 the digits 3 and 4 are in the correct positions. Thus, the program should respond with 2 (number of comect digits) and 7 (sum of the comect digits). Allow the user to guess until she 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 Conrect: Sum of Digits Correct 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 Please enter a 5-digit code (your guess) 11356 Number of Digits Correct: 1 Sum of Digits Correct Please enter a 5-digit code (your guess) 14292 Number of Digits Correct: 22 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 Conrect: 5 Sum of Digits Correct OORAY! 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. S170, Octavio

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!