如果语句跳过 || 如果第一个条件为假

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

If statement skipping || if first condition is false

问题

以下是您要翻译的内容:

我有一个搜索功能,它检查搜索关键字是否与对象属性匹配。在不遍历所有属性的情况下,如果第一个检查为假,它会停止。以下是代码示例:

else if (address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false
    || address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false
    || address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false
    || address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false
    || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);
}

因此,如果姓是"smith",名字是"sam",搜索关键字是"sam",在找到姓不等于"sam"后,它会停止。

以下是一个未通过测试的示例:

@Test
public void searchFirstNameTest() {
    Address address1 = new Address();
    address.setFirstName("sam");
    address.setLastName("smith");
    address1.setFirstName("bob");
    input.add(address);
    input.add(address1);

    List<Address> result = testee.searchAddress("sam", input);

    assertEquals(1, result.size());
    assertEquals("sam", result.get(0).getFirstName());
}

我在哪里出错了?

英文:

I have a search function that checks if the searchKey matches an Objects Attribute. Instead of going through all attributes it stops if the first check is false. Heres the code

else if (address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false
                    || address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false
                    || address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false
                    || address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false
                    || Integer.toString(address.getPlz()).equals(searchKey)) {
                outputList.add(address);

So if the lastname is smith and the firstname is sam, and the searchKey is sam, it will stop after finding that Lastname!= sam.

Test that fails:

 @Test
    public void searchFirstNameTest() {
        Address address1 = new Address();
        address.setFirstName(&quot;sam&quot;);
        address.setLastName(&quot;smith&quot;);
        address1.setFirstName(&quot;bob&quot;);
        input.add(address);
        input.add(address1);

        List&lt;Address&gt; result = testee.searchAddress(&quot;sam&quot;, input);

        assertEquals(1, result.size());
        assertEquals(&quot;sam&quot;, result.get(0).getFirstName());
    }

where am i going wrong?

答案1

得分: 3

解决方法可能是,您需要将您的语句放在括号中。

else if ((address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false)
                    || (address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false)
                    || (address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false)
                    || (address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false)
                    || Integer.toString(address.getPlz()).equals(searchKey)) {
                outputList.add(address);

否则,语句将不按正确顺序执行。

英文:

The solution may be, that you need to put your statements in brackets.

else if ((address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false)
                    || (address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false)
                    || (address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false)
                    || (address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false)
                    || Integer.toString(address.getPlz()).equals(searchKey)) {
                outputList.add(address);

Otherwise the statements are not executed in the right order.

答案2

得分: 1

> 我在哪里出错了?

你认为 A ? B : C || D ? E : F 的意思是 (A ? B : C) || (D ? E : F),然而运算符优先级决定它实际上表示:

A ? B : ( (C || D) ? E : F )

表达式应为:

else if ((address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false)
        || (address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false)
        || (address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false)
        || (address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false)
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

尽管最好不使用三元条件运算符来编写:

else if ((address.getLastName() != null &amp;&amp; address.getLastName().toLowerCase().contains(searchKey))
        || (address.getFirstName() != null &amp;&amp; address.getFirstName().toLowerCase().contains(searchKey))
        || (address.getTitle() != null &amp;&amp; address.getTitle().toLowerCase().contains(searchKey))
        || (address.getStreet() != null &amp;&amp; address.getStreet().toLowerCase().contains(searchKey))
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

虽然可以在不使用额外括号的情况下编写,但你不应该这样做

else if (address.getLastName() != null &amp;&amp; address.getLastName().toLowerCase().contains(searchKey)
        || address.getFirstName() != null &amp;&amp; address.getFirstName().toLowerCase().contains(searchKey)
        || address.getTitle() != null &amp;&amp; address.getTitle().toLowerCase().contains(searchKey)
        || address.getStreet() != null &amp;&amp; address.getStreet().toLowerCase().contains(searchKey)
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

在混合使用 &amp;&amp;||?: 运算符时,始终使用明确的括号。

英文:

> where am i going wrong?

You think that A ? B : C || D ? E : F means (A ? B : C) || (D ? E : F), however operator precedence dictates that it actually means:

A ? B : ( (C || D) ? E : F )

The expression should be:

else if ((address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false)
        || (address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false)
        || (address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false)
        || (address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false)
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

Although it is better written without using ternary conditional operator:

else if ((address.getLastName() != null &amp;&amp; address.getLastName().toLowerCase().contains(searchKey))
        || (address.getFirstName() != null &amp;&amp; address.getFirstName().toLowerCase().contains(searchKey))
        || (address.getTitle() != null &amp;&amp; address.getTitle().toLowerCase().contains(searchKey))
        || (address.getStreet() != null &amp;&amp; address.getStreet().toLowerCase().contains(searchKey))
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

Which can be written without the extra parentheses, but you shouldn't:

else if (address.getLastName() != null &amp;&amp; address.getLastName().toLowerCase().contains(searchKey)
        || address.getFirstName() != null &amp;&amp; address.getFirstName().toLowerCase().contains(searchKey)
        || address.getTitle() != null &amp;&amp; address.getTitle().toLowerCase().contains(searchKey)
        || address.getStreet() != null &amp;&amp; address.getStreet().toLowerCase().contains(searchKey)
        || Integer.toString(address.getPlz()).equals(searchKey)) {
    outputList.add(address);

Always use explicit parentheses when mixing &amp;&amp;, ||, and ?: operators.

答案3

得分: -1

|| 是懒惰的。如果左边为假,我们知道结果将为假,右边不会被评估。

我建议您重新编写逻辑,使用返回语句使其更简单。

英文:

|| is lazy. If the left side is false we know that the result will be false, and the right side is not evaluated.

I suggest you rewrite your logic to be simpler with returns instead.

huangapple
  • 本文由 发表于 2020年8月25日 20:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578634.html
匿名

发表评论

匿名网友

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

确定