简体   繁体   English

即使条件为假,if 语句也会执行

[英]if statement executing even if condition is false

I am trying: if abb.iscurrentlyy() returns true, then go inside the if statement or abb.islately() is true.我正在尝试:如果abb.iscurrentlyy()返回 true,则if语句或abb.islately()中的 go 为 true。

What is wrong with my if statement?我的 if 语句有什么问题? When I set abb.iscurrentlyy() to be false.当我将abb.iscurrentlyy()设置为 false 时。 abb.islately() is set to true. abb.islately()设置为 true。 Why is abb.iscurrentlyy() going inside the loop even abb.iscurrentlyy() is false?为什么abb.iscurrentlyy()会进入循环,即使abb.iscurrentlyy()是假的?

    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.
    }

The term or (||) means that for the conditional to be true at least one of the sub-conditions has to be true.术语or (||) 意味着要使条件为真,至少有一个子条件必须为真。

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 (&&).另一方面,如果当一个条件为真而另一个条件为假时您想要假,请使用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."想想or更宽容:“我可以让这个或那个是真的。” and , however, sounds inclusive, but it's actually more exclusive: "I have to have this AND that to be true." and ,然而,听起来包容,但它实际上更排他性:“我必须拥有这个和那个才是真实的。”

In your code both abb.isCurrentlyy() and abb.islately() needs to be false to not go inside loop.在您的代码中, abb.isCurrentlyy()abb.islately()都需要为 false,而不是 go 内循环。 || works as a logical OR.用作逻辑 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.我建议您为这两个条件编写单独的 if 循环。

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

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