如果语句执行,即使条件为假。

huangapple go评论79阅读模式
英文:

if statement executing even if condition is false

问题

如果 abb.iscurrentlyy() 返回 true,那么进入 if 语句或者 abb.islately() 为 true。

我的 if 语句有什么问题?当我将 abb.iscurrentlyy() 设置为 false 时,abb.islately() 被设置为 true。为什么 abb.iscurrentlyy() 在条件为 false 时仍然进入循环?

if (abb.iscurrentlyy() || abb.islately()) {

    if (reqFiles != null) {

        for (int i = 0; i < reqFiles.length; i++) {

            Future<ExecResult> lcukv = executor.submit(worker);
            executionFutureException.add(lcukv);
        }
    }

} else { // 当条件为 false 时执行。
}
英文:

I am trying: if abb.iscurrentlyy() returns true, then go inside the if statement or abb.islately() is true.

What is wrong with my if statement? When I set abb.iscurrentlyy() to be false. abb.islately() is set to true. Why is abb.iscurrentlyy() going inside the loop even abb.iscurrentlyy() is false?

	if (abb.iscurrentlyy() || abb.islately()) {

		if (reqFiles != null) {

			for (int i = 0; i < reqFiles.length; i++) {

				Future<ExecResult> lcukv = executor.submit(worker);
				executionFutureException.add(lcukv);
			}
		}

	} else { // do if condition is false.
	}

答案1

得分: 5

or(||)一词表示在条件为真时,至少有一个子条件必须为真。

boolean condition1 = true;
boolean condition2 = false;

if (condition1 || condition2) {
    System.out.println("其中一个或两个条件为真");
} else {
    System.out.println("两个条件都为假");
}

从现实世界的角度来看,想象走进一家冰淇淋店。你喜欢草莓冰淇淋,也喜欢巧克力冰淇淋,但讨厌其他所有口味。如果店里至少有一种你喜欢的口味,你就会高兴。

你的代码类似于上面的例子。另一方面,如果你希望在一个条件为真而另一个条件为假时得到假结果,可以使用 and(&&)。

boolean condition1 = true;
boolean condition2 = false;

if (condition1 && condition2) {
    System.out.println("两个条件都为真");
} else {
    System.out.println("至少有一个条件为假");
}

在这种情况下,冰淇淋店必须同时有你喜欢的两种口味,你才会高兴。

or 视为更宽松的:「我可以有这个或者那个为真。」然而,and 的含义更为独占,尽管听起来包容性更强:「我必须同时有这个和那个为真。」

英文:

The term or (||) means that for the conditional to be true at least one of the sub-conditions has to be true.

boolean condition1 = true;
boolean condition2 = false;

if(condition1 || condition2)
{
    System.out.println("One or both conditions were true");
}
else
{
    System.out.println("Both conditions were false");
}

In real world terms, imagine walking into an ice cream shop. You like strawberry ice cream. You also like chocolate ice cream. You hate all other flavors. If the shop has at least one of those flavors, you're happy.

Your code is analogous to the example above. If, on the other hand, you want false when one of the conditions is true and the other is false, use and (&&).

boolean condition1 = true;
boolean condition2 = false;

if(condition1 && condition2)
{
    System.out.println("Both conditions were true");
}
else
{
    System.out.println("At least one condition was false");
}

In this case, the ice cream shop has to have both flavors you like in order for you to be happy.

Think of or as being more permissive: "I can have this OR that to be true." and, however, sounds inclusive, but it's actually more exclusive: "I have to have this AND that to be true."

答案2

得分: 2

在您的代码中,abb.isCurrentlyy()abb.islately() 都需要为 false 才能不进入循环。|| 表示逻辑或,这意味着如果其中一个值为 true,则语句的值为 true。我建议您为这两个条件分别编写 if 循环。

英文:

In your code both abb.isCurrentlyy() and abb.islately() needs to be false to not go inside loop. || works as a logical OR. which means if either one of the value is true statement is evaluated as True. I suggest you to write individual if loop for these two condition.

huangapple
  • 本文由 发表于 2020年7月25日 10:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63083660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定