Java Else如果老虎机不检查所有数字是否相同

Java Else如果老虎机不检查所有数字是否相同,java,if-statement,Java,If Statement,我正在创建一个简单的老虎机程序,问题是else if语句没有检查三个数字是否相同。我不太清楚问题是什么,因为每当三个数字相同时,它只打印“两个数字匹配”,而不是说完全匹配 问题在于您组织条件的方式:每次有三个数字的匹配,也是两个数字的匹配,因此您应该将限制性更强的检查置于限制性较弱的检查之前: //All Matches if(slot1==slot2 && slot2==slot3) { System.out.println("Perfect Match!! You w

我正在创建一个简单的老虎机程序,问题是
else if
语句没有检查三个数字是否相同。我不太清楚问题是什么,因为每当三个数字相同时,它只打印“两个数字匹配”,而不是说完全匹配


问题在于您组织条件的方式:每次有三个数字的匹配,也是两个数字的匹配,因此您应该将限制性更强的检查置于限制性较弱的检查之前:

//All Matches
if(slot1==slot2 && slot2==slot3) {
    System.out.println("Perfect Match!! You win");
}else if (slot1 == slot2 || slot1==slot3||slot2==slot3){
    System.out.println("You got two matches!");
}else {
    System.out.println("No matches, you suck!");
}

问题在于您组织条件的方式:每次有三个数字的匹配,也是两个数字的匹配,因此您应该将限制性更强的检查置于限制性较弱的检查之前:

//All Matches
if(slot1==slot2 && slot2==slot3) {
    System.out.println("Perfect Match!! You win");
}else if (slot1 == slot2 || slot1==slot3||slot2==slot3){
    System.out.println("You got two matches!");
}else {
    System.out.println("No matches, you suck!");
}

如果您可以用普通语言阅读代码路径,则更容易发现这一点。您只需切换第一个和第二个条件即可解决此问题

I have numbers a,b,c

if a = b or a = c or b = c // This is also true in the case of all matches, so the code will stop executing this conditional here
    //I have two matches
else if a = b and b = c
    //I have all matches
else
    //have no matches

如果您可以用普通语言阅读代码路径,则更容易发现这一点。您只需切换第一个和第二个条件即可解决此问题

I have numbers a,b,c

if a = b or a = c or b = c // This is also true in the case of all matches, so the code will stop executing this conditional here
    //I have two matches
else if a = b and b = c
    //I have all matches
else
    //have no matches

先检查一下是否完美匹配。。。(如果你有一个完美的匹配,你总是会有两个相等的,这是先检查的)只要先检查完美匹配。。。(如果您有一个完美的匹配项,您将始终有两个相等的项,这两个项将首先被检查)“执行计划”,您是DBA,您是:)不,不,很好,我喜欢回答得好。“执行计划”,你是DBA,你是。:)不,不,很好,我喜欢回答得好。