逻辑或和逻辑或在Java混淆? [英] Logical OR and logical OR confounded by java?
问题描述
我用这个梅索德为了检查一个环节是有效的,但是当我用一个逻辑或不工作,但它的工作原理,当我使用逻辑和它的怪异。你觉得呢?
公共字符串验证(){
。字符串留置权= URL.getText()的toString();
如果(titre.getText()。的toString()的isEmpty())
{
返回titreEmpty
}
否则如果(lien.isEmpty()){
返回UrlEmpty
}否则如果(!lien.contains(HTTP)){
返回notALink
}否则如果(!lien.contains(网盘)||!lien.contains(统称YouTube)){//逻辑或不工作
返回linkInvalid
} 返回苯教
在你的情况请看下图:
!lien.contains(网盘)|| !lien.contains(YouTube的)
在情况下,当留置权
不包含的SkyDrive
?结果会发生什么
整个状况进行评估,以</ P>
真||随你
将被评估为真,不管留置权
将包含的YouTube
或没有(这将重新presents 任何
)。
也是一样的的YouTube
。如果留置权
将不包含的YouTube
,条件必须进行评估,以<code>真正,因为这将是无论||真正
。
如果你想写条件是会说
,如果它是不正确的行包含`skydrive`或行包含`youtube`
^^^^^^^^^^^^^^(^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^)
如果((A |!b)条)
您需要把它写成
如果(!(line.contains(网盘)|| line.contains(统称YouTube)))
或使用德摩根定律 (A |!b )
&LT; ==> A和!&安培; !b
像
如果(!line.contains(网盘)及和放大器;!line.contains(统称YouTube))
和在这里你有德·摩根定律的一点证据,其中 1 =真
, 0 = FALSE
A | C | !一个| !C | A | B | !(A | B)| !A和!b
- + --- + ---- + ---- + ----- + -------- + ---------
0 | 0 | 1 | 1 | 0 | 1 | 1
0 | 1 | 1 | 0 | 1 | 0 | 0
1 | 0 | 0 | 1 | 1 | 0 | 0
1 | 1 | 0 | 0 | 1 | 0 | 0
这显示了每个案件的结果 A
和 B
是相同!(A | b)
和 A&放!; !b
I use this methode in order to check if a link is valid but when I use a logical OR it doesn't work but it works when i use a logical AND it's weird. What do you think?
public String verification() {
String lien = URL.getText().toString();
if(titre.getText().toString().isEmpty())
{
return "titreEmpty";
}
else if (lien.isEmpty()) {
return "UrlEmpty";
} else if (!lien.contains("http")) {
return "notALink";
} else if (!lien.contains("skydrive") || !lien.contains("youtube") ) { //the logical OR doesn't work
return "linkInvalid";
}
return "bon";
Take a look at your condition:
!lien.contains("skydrive") || !lien.contains("youtube")
What happens in case when lien
don't contain skydrive
?
Entire condition is evaluated to
true || whatever
which will be evaluated to true, regardless if lien
will contain youtube
or not (which represents whatever
).
Same goes for youtube
. If lien
will not contain youtube
, condition must be evaluated to true
because it will be whatever || true
.
If you want to write condition that will say
if it is not true that line contains `skydrive` or line contains `youtube`
^^^^^^^^^^^^^^ ( ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ )
if( ! ( a | b ) )
you need to write it as
if ( ! ( line.contains("skydrive") || line.contains("youtube") ) )
or using De Morgan's laws !( a | b )
<==> !a && !b
like
if ( !line.contains("skydrive") && !line.contains("youtube") )
and here you have little proof of De Morgan's law where 1 = true
, 0 = false
a | b | !a | !b | a|b | !(a|b) | !a & !b
--+---+----+----+-----+--------+---------
0 | 0 | 1 | 1 | 0 | 1 | 1
0 | 1 | 1 | 0 | 1 | 0 | 0
1 | 0 | 0 | 1 | 1 | 0 | 0
1 | 1 | 0 | 0 | 1 | 0 | 0
which shows that result of each case of a
and b
is the same for !(a|b)
and !a & !b
这篇关于逻辑或和逻辑或在Java混淆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!