有什么不对这个逻辑声明(Android版)? [英] What is wrong with this logic statement (Android)?

查看:188
本文介绍了有什么不对这个逻辑声明(Android版)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么下面的逻辑是行不通的:

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

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

所以我想打印的'如果光标数超过1,但只有当组件类型不等于x或y。

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

似乎对于那些年,但不是X有趣的情况下工作。

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

Confused.com! :)

Confused.com! :)

推荐答案

尝试

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.

我相信这些更加紧密matche你想要什么你的英文说明。

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

编辑:

要清理的混乱,让我们来分析您的英文说明的最后一部分的逻辑:

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

...但只有当组件类型不等于x或y。

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

说出同样的事情的另一种方式是组件类型是x和y都不。为了转化为code这一点,我们应该更进一步,并改写这种情况为这并不是说任何的componentType是X或comonentType为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)

这是从原来的code这是形式有很大不同。

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.

分析逻辑另一种方法是看code你给了:

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

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

让我们看几个例子:


  1. X.equals(组件类型)是真实的。这意味着否定是假的。这也意味着,Y.equals(组件类型)'是假的的的否定是真实的。因此,您的code计算为真。

  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.

Y.equals(组件类型)是真实的。这意味着否定是假的。这也意味着,X.equals(组件类型)'是假的的的否定是真实的。因此,您的code计算为真。

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

无论是X.equals(组件类型),也不Y.equals(组件类型)是真实的。这意味着这两个否定都是假的,你的code计算为false。

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

请注意,您的code计算结果为真在这两种情况下1和2。这不会产生相同的结果从你的英文说明的预期。

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.

这篇关于有什么不对这个逻辑声明(Android版)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆