What is wrong with this logic statement (Android)?

浪子不回头ぞ 提交于 2019-12-12 01:27:21

问题


I can't see why the following logic isn't working:

                if (cursorCount > 1 && (!"x".equals(componentType) || !"y".equals(componentType))){
                        message.append("s");
                    }

So I want to to print 's' if the cursor count is over 1 but only when the componentType does not equal x or y..

Seems to work for the cases that are y but not x interestingly.

Confused.com! :)


回答1:


Try

if (cursorCount > 1 && !("x".equals(componentType) || "y".equals(componentType)))

Equivalently you can do

if (cursorCount > 1 && !"x".equals(componentType) && !"y".equals(componentType))

This comes from an application of deMorgan's law to your logic.

I believe these more closely matche your English description of what you want.

Edit:

To clear up the confusion, let's analyze the logic of the final part of your English description:

...but only when the componentType does not equal x or y.

Another way to state the same thing is "componentType is neither x nor y". In order to translate this into code, we should go a step further and reword this condition as "It is not the case that either componentType is x or comonentType is y." This final version suggests that the correct boolean formula is of the form

!(A || B)

This is very different from your original code which is of the form

!A || !B

Note that my final rewording is more verbose, but the extra verbiage makes the logic more clear.

Another way to analyze the logic is to look at the code you gave:

!"x".equals(componentType) || !"y".equals(componentType)

Let's look at a few examples:

  1. "x".equals(componentType) is true. This means the negation is false. It also means that"y".equals(componentType)` is false and it's negation is true. Therefore, your code evaluates to true.

  2. "y".equals(componentType) is true. This means the negation is false. It also means that"x".equals(componentType)` is false and it's negation is true. Therefore, your code evaluates to true.

  3. Neither "x".equals(componentType) nor"y".equals(componentType) is true. This means both negations are false and your code evaluates to false.

Note that your code evaluates to true in both cases 1. and 2. This does not give the same result as expected from your English description.




回答2:


You have a '(' before your "x" condition that shouldn't be there. Then remove one of them from the end of the whole if statement. You should wrap those conditions that belong together in "()" because it is confusing as heck to read like this.

if ((cursorCount > 1 && !"x".equals(componentType)) || (!"y".equals(componentType)))




回答3:


if ((cursorCount > 1 && !"x".equals(componentType)) 
|| (cursorCount > 1 && !"y".equals(componentType))) 
  message.append("s");

or you could nest them if it'd make it easier

if (cursorCount > 1)
  if (componentType!="y" || componentType!="x")
    message.append("s"); 

This would make it easier to understand and with less fallacies.



来源:https://stackoverflow.com/questions/14267678/what-is-wrong-with-this-logic-statement-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!