Nam Duong
Nam Duong

Reputation: 23

my guessing game counter keeps rising

so for class I'm supposed to make a guessing game that gives you clues as you get closer to the answer. My question is when i run it and i get one Number correct, I would obviously keep that number and keep going with the other 4 numbers, when I do that, the problem is my correct digits counter keeps rising even if I don't get other digits correct.. how would I remedy this? Would i be able to add breaks in each of the if statements or would that completely exit me out of my do while loop?

public class GuessingGame {

public static void main(String[] args) {
    int guess,numDigitsCorrect=0,sumDigitsCorrect=0,attempts=0,answer;
    Random rng = new Random();
    Scanner consoleScanner = new Scanner(System.in);
    answer = rng.nextInt(90000) + 10000;
    System.out.println("I have randomly chosen a 5-digit code for you to guess.Each time you guess,\n"
            +   "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:\n"
            +   "Number of Digits Correct: 1\n"
            +   "Sum of Digits Correct: 4\n"
            +   "From deduction, you will know the 4 was correct in the guess."
            +   "\nNow its your turn..................................................................");
    do{
        System.out.print("Please enter a 5-digit code (your guess): ");
        guess = consoleScanner.nextInt();
        int g1 = guess/10000;
        int g2 = guess%10000/1000;
        int g3 = guess % 10000 % 1000 / 100;
        int g4 = guess % 10000 % 100 /10;
        int g5 = guess % 10000 % 10 / 1;
        int a1 = answer/10000;
        int a2 = answer%10000/1000;
        int a3 = answer % 10000 % 1000 / 100;
        int a4 = answer % 10000 / 100 / 10;
        int a5 = answer % 10000 % 10 / 10;
        if(g1 == a1)
        {
            numDigitsCorrect ++;
            sumDigitsCorrect += a1;
            System.out.println("\nNumber of digits correct: " + numDigitsCorrect) ;
            System.out.println("Sum of digits correct: " + sumDigitsCorrect);
            System.out.println();
        }
        if(g2 == a2)
        {
            numDigitsCorrect ++;
            sumDigitsCorrect += a2;
            System.out.println("Number of digits correct: " + numDigitsCorrect) ;
            System.out.println("Sum of digits correct: " + sumDigitsCorrect);
            System.out.println();
        }
        if (g3 == a3)
        {
            numDigitsCorrect ++;
            sumDigitsCorrect += a3;
            System.out.println("Number of digits correct: " + numDigitsCorrect) ;
            System.out.println("Sum of digits correct: " + sumDigitsCorrect);
            System.out.println();
        }
        if (g4 == a4)
        {
            numDigitsCorrect ++;
            sumDigitsCorrect += a4;
            System.out.println("Number of digits correct: " + numDigitsCorrect) ;
            System.out.println("Sum of digits correct: " + sumDigitsCorrect);
            System.out.println();
        }
        if (g5 == a5)
        {
            numDigitsCorrect ++;
            sumDigitsCorrect += a5;
            System.out.println("Number of digits correct: " + numDigitsCorrect) ;
            System.out.println("Sum of digits correct: " + sumDigitsCorrect);
            System.out.println();
        }
        if(guess == answer)
        {
            System.out.println("****HOORAY!  You solved it.  You are so smart****");
            break;
        }
    }while (guess != answer);

}

}

Upvotes: 0

Views: 40

Answers (1)

Naman
Naman

Reputation: 31928

Few things to fix -

  1. Make sure your a4, a5 are correct

    int a4 = answer % 10000 % 100 / 10; // note the modulus
    
    int a5 = answer % 10000 % 10; // note divided by 1 or remove the redundant statement
    
  2. Move your print statement out of your if block to the end of all if inside the do block as -

    if (g1 == a1) {
            numDigitsCorrect++;
            sumDigitsCorrect += a1;
    }
    ... //other if statements
    if (guess == answer) {
        System.out.println("****HOORAY!  You solved it.  You are so smart****");
        break;
    }
    System.out.println("Number of digits correct: " + numDigitsCorrect);
    System.out.println("Sum of digits correct: " + sumDigitsCorrect);
    
  3. Also since you already do a check

    if (guess == answer) {
        System.out.println("****HOORAY!  You solved it.  You are so smart****");
        break;
    }
    

    within your do you can change your while condition to true as -

    do {
     ... your existing code
     } while(true);
    
  4. To answer

Would i be able to add breaks in each of the if statements

If you do so, for even a single digit match your loop will exit(break).

  1. Importantly to fix the counter, initialize the counter within the do block as

    do {
        numDigitsCorrect = 0;
        sumDigitsCorrect = 0;
    .. // existing logic
    }
    

Upvotes: 1

Related Questions