简体   繁体   English

“如果(int == 1)”不起作用

[英]“if (int == 1)” doesn't work

So I am trying to learn Java, and have been writing a basic program based off of the 21 matches game; 因此,我正在尝试学习Java,并且已经基于21个比赛游戏编写了一个基本程序; in which each player takes turns taking 1,2 or 3 matches. 每个玩家轮流参加1,2或3场比赛。 The player to take the last match loses. 参加最后一场比赛的玩家输了。

It was going well until I found that when the user was about to win, the program wouldn't enter the final else if statement; 一切顺利,直到我发现当用户即将获胜时,该程序才不会进入最终的else if语句。 it would skip over it and come back to the user, without the computer having taken any matches. 它会跳过它并返回给用户,而计算机不会进行任何匹配。 Attached is the method I have written for the computers turn. 附件是我为计算机转写的方法。

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches < 5){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}

The line else if (matches == 1){ matchesTaken = 1; } 该行else if (matches == 1){ matchesTaken = 1; } else if (matches == 1){ matchesTaken = 1; } is where I'm having a problem. else if (matches == 1){ matchesTaken = 1; }是我遇到问题的地方。 Any help would be appreciated. 任何帮助,将不胜感激。

PS I realize that I could turn the else if into an else statement, but the main thing is I want to learn what the problem is, not just get around it. PS:我意识到我可以将else if转换为else语句,但是最主要的是我想了解问题所在,而不仅仅是解决它。

You should move the (matches == 1) before (matches < 5) condition. 您应在(matches < 5)条件之前移动(matches == 1) The code is not reachable till (matches == 1) as this condition is satisfied by (matches < 5) also. 直到(matches == 1)都无法访问该代码,因为此条件也由(matches < 5)满足。

Updated code: 更新的代码:

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches == 1){ matchesTaken = 1; }
    else if (matches < 5){ matchesTaken = matches - 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}

the matches < 5 will Always be reached Before the matches == 1 meaning it will never trigger the second if. 匹配数<5将始终在匹配== 1之前到达,这意味着它将永远不会触发第二个if。 so either you change order of the if's or add a Another argument to your less than 5 ie (matches < 5 && matches > 1). 因此,您可以更改if的顺序,或者在小于5的ie中添加另一个参数(匹配数<5 &&匹配数> 1)。

The condition (matches == 1) will be unreachable since your previous condtion is always makes your later condition true ie., if (matches < 5) which actually considers matches == 1 . 条件(matches == 1)将不可用,因为您的上一个条件始终会使您以后的条件成立,即, if (matches < 5)实际上认为matches == 1 You can make the last two condition as: 您可以将最后两个条件设为:

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

or like 或喜欢

else if (matches < 5 && matches > 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

Your problem is that the statement 您的问题是该声明

else if (matches < 5){ matchesTaken = matches - 1; }

will be true when the matches is 1. Thus the else if will never be entered. 当匹配为1时将为true。因此,永远不会输入else if。 You can change it to 您可以将其更改为

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }

That way, it will not trigger when the number of matches is 1 and the following block can execute successfully. 这样,当匹配数为1并且下一个块可以成功执行时,它将不会触发。

Put

else if (matches == 1){ matchesTaken = 1; }

before 之前

else if (matches < 5){ matchesTaken = matches - 1; }

The order is important. 顺序很重要。 In IF_ELSE, once a match is occurs it doesn't check the next condition. 在IF_ELSE中,一旦发生匹配,就不会检查下一个条件。 In your case as matches<5 is true it won't check matches==1 . 在您的情况下, matches<5为true时,将不检查matches==1

You've already checked else if (matches < 5) then it's useless checking 您已经检查过else if (matches < 5)那么它是无用的检查
else if (matches == 1) , because above condition will end the if chain. else if (matches == 1) ,因为上述条件将终止if链。 So you can change else if (matches < 5) to else if (matches < 5 && matches !=1) to make the last statement also reachable. 因此,您可以将else if (matches < 5)更改为else if (matches < 5 && matches !=1)以使最后一条语句也可访问。

When ever you use else if, use equal conditions (==) first then go for greater than or equal (X>=Y) or less than or equal(X<=Y) conditions. 如果您使用else if,则首先使用相等条件(==),然后使用大于或等于(X> = Y)或小于或等于(X <= Y)的条件。

The statement which satisfies the condition first will execute and rest of else if statements will ignore. 首先满足条件的语句将被执行,如果语句将被忽略,则其余条件将被执行。

Happy Coding! 编码愉快!

if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
else if (matches < 5){ matchesTaken = matches - 1; }
else if (matches == 1){ matchesTaken = 1; }

In your code first execute else if (matches < 5){ matchesTaken = matches - 1; } 在您的代码中首先执行else if (matches < 5){ matchesTaken = matches - 1; } else if (matches < 5){ matchesTaken = matches - 1; }

This line execute first and this line also satisfy if matches == 1 , thats why it will never goes to the next line ( else if (matches == 1){ matchesTaken = 1; } ) when matches == 1 该行执行第一,这条线也满足了,如果matches == 1 ,这就是为什么它永远不会进入到下一行( else if (matches == 1){ matchesTaken = 1; }matches == 1

1 is less than 5. This means that when matches is equal to one, it will always be captured in the statement "else if (matches < 5){ matchesTaken = matches - 1; }". 1小于5。这意味着当matchs等于1时,它将始终在语句“否则,如果(matches <5){matchesTaken = matchs-1;}”中捕获。

This is the reason why (matches == 1) is never executed. 这就是从未执行(matches == 1)的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM