麻烦我的if,else语句

时间:2016-10-03 19:57:32

标签: java bluej

我无法弄清楚为什么我的程序会跳到最后的#34;否则"声明。这是我的代码如下,也有点粗糙,因为我刚开始它,我是Java的新手。如果还有什么需要你问一下!

我们应该编写的程序用于获取储物柜组合。我已经硬编码了我想要的组合,当我输入12-34-56时程序可以工作。但是,如果我输入12-12-34或任何其他变体,它会自动跳到程序结尾并说不匹配,即使这不应该是结果。相反它应该打印出来#猜测中的2个数字是彼此重复的。组合中出现2个数字。"

如果您需要其他任何内容,请告诉我!谢谢,祝你有个美好的一天! (我也使用java和BlueJ来完成所有这些。)

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


    }
}

1 个答案:

答案 0 :(得分:1)

最后两个else-if-conditions有两个问题:

    需要交换
  1. lockguard。我们总是想检查所有三个猜测。目前,一次猜测被检查两次/三次,并且忽略一次/两次猜测。特别是第二个最后一个 - 如果只能在lock1 = lock2 = lock3时输入。另一种发现问题的方法:要知道Guess contains 3 duplicate numbers.你必须检查所有三个猜测。 Currenlty,只检查一个猜测(每个案例)。

  2. 最后的else-if-condition缺少一些分离。有18种可能的情况输入两次相同的猜测,程序只检查其中的12种。

  3. 以下是目前和遗失的案例。每个数字代表三个猜测中的一个。

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

    在我将第二个else-if-condition更改为

    之后,程序是正确的*
       guess1 == lock1 && guess2 == lock1 && guess3 == lock1
    || guess1 == lock2 && guess2 == lock2 && guess3 == lock2
    || guess1 == lock3 && guess2 == lock3 && guess3 == lock3
    

    的最后一个if-if条件
       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
    

    *我使用脚本检查了所有可能的组合。

    提示:如果您使用数组和循环,程序将更容易,更清晰,更可扩展。