簡體   English   中英

即使條件為假,if 語句也會執行

[英]if statement executing even if condition is false

我正在嘗試:如果abb.iscurrentlyy()返回 true,則if語句或abb.islately()中的 go 為 true。

我的 if 語句有什么問題? 當我將abb.iscurrentlyy()設置為 false 時。 abb.islately()設置為 true。 為什么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.
    }

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

在現實世界中,想象走進一家冰淇淋店。 你喜歡草莓冰淇淋。 你也喜歡巧克力冰淇淋。 你討厭所有其他口味。 如果這家商店至少有其中一種口味,那你就很高興了。

您的代碼類似於上面的示例。 另一方面,如果當一個條件為真而另一個條件為假時您想要假,請使用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");
}

在這種情況下,冰淇淋店必須有你喜歡的兩種口味才能讓你開心。

想想or更寬容:“我可以讓這個或那個是真的。” and ,然而,聽起來包容,但它實際上更排他性:“我必須擁有這個和那個才是真實的。”

在您的代碼中, abb.isCurrentlyy()abb.islately()都需要為 false,而不是 go 內循環。 || 用作邏輯 OR。 這意味着如果任何一個值為真,則語句被評估為真。 我建議您為這兩個條件編寫單獨的 if 循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM