If语句中的OR运算符


else if (!emailGet.endsWith(".com") && !emailGet.endsWith(".info")){ 
errors += "Email should end with .info or .com";
}

为什么CCD_ 1扮演着";OR";语句,但当我使用";OR";它本身什么也不做。我能让代码告诉我一个或另一个语句是真的唯一方法是使用&&,它计算两个不同于"1"的语句;OR";,使用&&背后的逻辑对我来说毫无意义。我是不是错过了什么?

注意以下关于||&&运算符的概念:

  1. 当多个条件与&&结合时,只要条件评估为true,条件的评估就继续。如果任何条件计算为false,则停止进一步的计算,并且组合结果为false。只有当所有条件都评估为&&1时,组合才会产生true
  2. 当多个条件与||组合时,只要条件评估为false,条件的评估就继续。如果任何条件评估为true,则停止进一步评估,并且组合结果为true。只有当所有条件评估为false时,组合才会产生false

基于这些概念,

!emailGet.endsWith(".com") && !emailGet.endsWith(".info")

与相同

!(emailGet.endsWith(".com") || emailGet.endsWith(".info"))

让我们在以下场景中分析它们:

假设emailGet="a@b.com">

CCD_ 18=>CCD_ 19=>CCD_ 20=>false

CCD_ 22=>CCD_ 23=>CCD_ 24=>false

假设emailGet="a@b.info">

CCD_ 26=>CCD_ 27=>CCD_ 28=>CCD_ 29=>CCD_ 30。

CCD_ 31=>CCD_ 32=>CCD_ 33=>CCD_ 34。

假设emailGet="a@b.c">

CCD_ 35=>CCD_ 36=>CCD_ 37=>CCD_ 38=>CCD_ 39。

CCD_ 40=>CCD_ 41=>CCD_ 42=>CCD_ 43。

我认为与(&&)以及组合的否定(||)引起误解。

if (!emailGet.endsWith(".com") && !emailGet.endsWith(".info")) {      
errors += "Email should end with .info or .com";
}
if (emailGet.endsWith(".com") || emailGet.endsWith(".info")) {      
sucesses += "Email did end with .info or .com";
}
if (!(emailGet.endsWith(".com") || emailGet.endsWith(".info"))) {      
errors += "Email should end with .info or .com";
}

它总是:

! <this-case> && ! <other-case>
<this-case> || <other-case>

你应该看吗

! <this-case> || ! <other-case> // *** ERROR *** always true
<this-case> && <other-case>     // *** ERROR *** always false

你知道这是错误的。

是的,在java中,条件的布尔运算符是||。(由两个竖条或"管道"表示,而不是小写L)类似地,您已经找到了条件的布尔运算符&&。这两种说法并不相同,尽管当两种说法都为真时,它们都会被评估为真。

最新更新