【问题标题】:Java - Condition is always True/False (Android Checkboxes)Java - 条件始终为真/假(Android 复选框)
【发布时间】:2018-08-25 21:19:45
【问题描述】:

我有以下代码来根据我的 Android 应用中选中的复选框显示某个字符串:

public String createOrderSummary(){

    CheckBox whippedCheckBox = findViewById(R.id.whipped);
    boolean whippedCream = whippedCheckBox.isChecked();

    CheckBox chocoBox = findViewById(R.id.chocolate);
    boolean chocolate = chocoBox.isChecked();

    if (whippedCream && chocolate){
        return "both selected";
    }else if(whippedCream || chocolate){
        if (whippedCream){
            return "whippedcream";
        }else if (chocolate){
            return "chocolate";
        }
    }else{
        return "none checked";
    }
    return "";
}

我在line 14 收到警告说condition chocolate is always true,这是为什么呢?

当我换行时:

 if (whippedCream){
        return "whipped cream";
    }else if(whippedCream && chocolate){
        return "both selected";
    }else{
        return "none selected";
    }

我在line 3 收到警告说condition always false

【问题讨论】:

  • 第 14 行是什么,第 3 行是什么?在第二个示例中,我假设第 3 行是从顶部算起的第三行?
  • 在第一个示例中,您的嵌套语句是不必要的,应该写成一个明确的 if、else if、else if、else 字符串。在第二个示例中,您需要首先检查最受约束的条件(&& 条件),然后是其他条件。这类似于常见的 Fizz Buzz 面试问题。

标签: java android if-statement checkbox conditional-statements


【解决方案1】:

让我们考虑一下您的部分情况:

if (whippedCream || chocolate) { // either whippedCream is true or chocolate is true
    if (whippedCream) { // whippedCream is true
        return "whippedcream";
    } else if (chocolate) { // whippedCream is not true, so chocolate must be true
        return "chocolate";
    }
}

因此这个条件可以简化:

if (whippedCream || chocolate) { // either whippedCream  is true or chocolate is true
    if (whippedCream) { // whippedCream is true
        return "whippedcream";
    } else { // chocolate must be true
        return "chocolate";
    }
}

当然,完全条件可以通过消除内部条件进一步简化:

if (whippedCream && chocolate) { // both true
    return "both selected";
} else if (whippedCream) { // only whippedCream is true
    return "whippedcream";
} else if (chocolate) { // only chocolate is true
    return "chocolate";
} else {
    return "none checked";
}

您的替代条件:

if (whippedCream){
    return "whipped cream";
}else if(whippedCream && chocolate){
    return "both selected";
}else{
    return "none selected";
}

完全是错误的。如果whippedCream 为真,你永远不会检查whippedCreamchocolate 是否都为真,因为else if 的条件只有在所有前面的条件都为假时才会被评估。

【讨论】:

    【解决方案2】:

    让我们考虑代码

    }else if(whippedCream || chocolate){
        if (whippedCream){
            return "whippedcream";
        }else if (chocolate){
            return "chocolate";
        }
    

    如果whippedCreamchocolatetrue,我们只会检查if (whippedCream)。所以,当我们到达else if (chocolate) 时,我们就知道whippedCreamfalse,否则我们就不会在第二个else 中。我们也知道chocolate 是真的,否则我们就不会在这个区块中。

    【讨论】:

      【解决方案3】:

      该消息是因为您如何嵌套 ifs。仅当其中之一为真时,您才输入if (whippedCream || chocolate)。然后,当一个简单的 else 就足够时,您可以单独检查每个。

      【讨论】:

        【解决方案4】:

        这是合乎逻辑的现象:

        如果此条件为真:if(whippedCream || chocolate) 这意味着其中一个 whippedCreamchocolatetrue 或两者兼而有之。

        所以里面有if (whippedCream) else if(chocolate),这意味着else if中的第二个if是不必要的。因为其中之一是true,所以如果(whippedCream)false,那么else表示巧克力是true,而if(chocolate)这个条件是不必要的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-14
          • 2016-09-01
          • 2017-10-14
          • 1970-01-01
          • 2014-05-10
          • 2015-04-23
          • 1970-01-01
          相关资源
          最近更新 更多