Search code examples
javabluej

Trouble with my if, else statements


I can not figure out why my program is skipping to the end to the final "else" statement. This is my code as follows, also its a little rough because I just started it and I am new to Java. If there is anything else you need just ask!

The program we are supposed to write is being used to get a locker combination. I have hard coded what I want the combination to be and the program works when I input 12-34-56. But if I input 12-12-34 or any other variation it automatically skips to the end of the program and says Not a match, even though that shouldn't be the outcome. Instead it should print out "2 numbers in the guess are duplicates of each other. 2 numbers appear in the combination."

Let me know if you require anything else! Thanks and have a great day! (Also I am using java and BlueJ to do all of this.)

import java.util.*;
public class Combination
{

public static void main(String[] args) {

        int lock1 = 12;
        int lock2 = 34;
        int lock3 = 56;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter first two digits: ");
        int guess1 = input.nextInt();
        System.out.print("Enter second two digits: ");
        int guess2 = input.nextInt();
        System.out.print("Enter last two digits: ");
        int guess3 = input.nextInt();

        if (lock1 == guess1 && lock2 == guess2 && lock3 == guess3) {
            System.out.print("Exact Match! Locker Unlocked");
        }
        else if (lock1 == guess1 && lock2 == guess3 && lock3 == guess2
        || lock1 == guess2 && lock2 == guess3 && lock3 == guess1 
        || lock1 == guess2 && lock2 == guess1 && lock3 == guess3
        || lock1 == guess3 && lock2 == guess2 && lock3 == guess1
        || lock1 == guess3 && lock2 == guess1 && lock3 == guess2)
        {
            System.out.println("All numbers match but not in the correct order");
        }
        else if (lock1 == guess1 && lock2 == guess1 && lock3 == guess1
        || lock1 == guess2 && lock2 == guess2 && lock3 == guess2
        || lock1 == guess3 && lock2 == guess3 && lock3 == guess3) {
            System.out.println("Guess contains 3 duplicate numbers.");
            System.out.println("One number in the guess appears in the combination.");
        }
        else if (lock1 == guess1 && lock2 == guess1 && lock3 == guess2
        || lock1 == guess1 && lock2 == guess1 && lock3 == guess3
        || lock1 == guess2 && lock2 == guess2 && lock3 == guess3
        || lock1 == guess2 && lock2 == guess2 && lock3 == guess1
        || lock1 == guess3 && lock2 == guess3 && lock3 == guess2
        || lock1 == guess3 && lock2 == guess3 && lock3 == guess1
        || lock1 == guess2 && lock2 == guess1 && lock3 == guess1
        || lock1 == guess3 && lock2 == guess1 && lock3 == guess1        
        || lock1 == guess3 && lock2 == guess2 && lock3 == guess2
        || lock1 == guess1 && lock2 == guess2 && lock3 == guess2
        || lock1 == guess1 && lock2 == guess3 && lock3 == guess3
        || lock1 == guess2 && lock2 == guess3 && lock3 == guess3) {
            System.out.println("2 numbers in the guess are duplicates of each other." +
            "2 numbers guess appear in the combination.");
        }
        else{
        System.out.println("Sorry not a match");
        }


    }
}

Solution

  • There are two problems with the two last else-if-conditions:

    1. lock and guard need to be swapped. We always want to check all three guesses. Currently, one guess is checked twice/thrice and one/two guesses are neglected. Especially the second last else-if can only be entered if lock1 = lock2 = lock3. Another way to spot the problem: To know that Guess contains 3 duplicate numbers. you have to check all three guesses. Currenlty, only one guess is checked (per case).

    2. The last else-if-condition is missing some disjuncts. There are 18 possible cases to enter exactly two times the same guess, the program only checks 12 of them.

    Here are the present and missing cases. Each digit represent one of the three guesses.

    missing|present
    -------+-------
            112
            113
    121
            122
    131
            133
            211
    212
            221
            223
    232
            233
            311
    313
            322
    323
            331
            332
    

    The program was correct* after I changed the second last else-if-condition to

       guess1 == lock1 && guess2 == lock1 && guess3 == lock1
    || guess1 == lock2 && guess2 == lock2 && guess3 == lock2
    || guess1 == lock3 && guess2 == lock3 && guess3 == lock3
    

    and the last else-if-condition to

       guess1 == lock1 && guess2 == lock1 && guess3 == lock2
    || guess1 == lock1 && guess2 == lock1 && guess3 == lock3
    || guess1 == lock1 && guess2 == lock2 && guess3 == lock1
    || guess1 == lock1 && guess2 == lock2 && guess3 == lock2
    || guess1 == lock1 && guess2 == lock3 && guess3 == lock1
    || guess1 == lock1 && guess2 == lock3 && guess3 == lock3
    || guess1 == lock2 && guess2 == lock1 && guess3 == lock1
    || guess1 == lock2 && guess2 == lock1 && guess3 == lock2
    || guess1 == lock2 && guess2 == lock2 && guess3 == lock1
    || guess1 == lock2 && guess2 == lock2 && guess3 == lock3
    || guess1 == lock2 && guess2 == lock3 && guess3 == lock2
    || guess1 == lock2 && guess2 == lock3 && guess3 == lock3
    || guess1 == lock3 && guess2 == lock1 && guess3 == lock1
    || guess1 == lock3 && guess2 == lock1 && guess3 == lock3
    || guess1 == lock3 && guess2 == lock2 && guess3 == lock2
    || guess1 == lock3 && guess2 == lock2 && guess3 == lock3
    || guess1 == lock3 && guess2 == lock3 && guess3 == lock1
    || guess1 == lock3 && guess2 == lock3 && guess3 == lock2
    

    * I checked all possible combinations using a script.

    Hint: The program would be easier, cleaner, and extendable if you used arrays and loops instead.