【问题标题】:OR operator inside of If statementsIf 语句中的 OR 运算符
【发布时间】:2021-02-27 22:07:59
【问题描述】:
else if (!emailGet.endsWith(".com") && !emailGet.endsWith(".info")){ 
    errors += "Email should end with .info or .com";
}

为什么&& 扮演“OR”语句的角色,但是当我使用“OR”本身时它什么也不做。我可以让代码告诉我一个或另一个语句是否为真的唯一方法是使用 && 评估与“OR”不同的 2 个语句,使用 && 背后的逻辑对我来说毫无意义。我错过了什么吗?

【问题讨论】:

  • 您没有复制所有代码!
  • && 根本没有玩 OR;你需要的是一个有效的&& 所以一个AND。使用 OR,它总是正确的:因为它总是与一个选项不同
  • 逻辑(按位)“与”和“或”的运算符分别为&|。此外,运算符&&|| 是快捷等效项:如果&& 的左侧是false,则表达式将计算为false,而右侧将不计算;如果|| 的左侧是true,则表达式将计算为true,而不会计算右侧。
  • How to make logical OR with AND,and NOT? 在同一站点上((发布代码与!(emailGet.endsWith(".com") || emailGet.endsWith(".info") 相同))
  • 这能回答你的问题吗? How to make logical OR with AND,and NOT?

标签: java if-statement syntax


【解决方案1】:

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

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

基于这些概念,

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

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

我们分以下几个场景来分析:

假设 emailGet = "a@b.com"

!emailGet.endsWith(".com") && !emailGet.endsWith(".info") => !(true) && !emailGet.endsWith(".info") => false && !emailGet.endsWith(".info") => false

!(emailGet.endsWith(".com") || emailGet.endsWith(".info")) => !(true || emailGet.endsWith(".info")) => !(true) => false

假设 emailGet = "a@b.info"

!emailGet.endsWith(".com") && !emailGet.endsWith(".info") => !(false) && !emailGet.endsWith(".info") => true && !(true) => true && false => false.

!(emailGet.endsWith(".com") || emailGet.endsWith(".info")) => !(false || true) => !(true) => false

假设 emailGet = "a@b.c"

!emailGet.endsWith(".com") && !emailGet.endsWith(".info") => !(false) && !emailGet.endsWith(".info") => true && !(false) => true && true => true.

!(emailGet.endsWith(".com") || emailGet.endsWith(".info")) => !(false || false) => !(false) => true

【讨论】:

    【解决方案2】:

    我认为否定与 and (&&) 和 or (||) 结合使用 造成误会。

        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
    

    你知道这是错的。

    【讨论】:

      【解决方案3】:

      是的,在 java 中,条件 or 的布尔运算符是 ||。(由两个竖线或“管道”表示,而不是小写 L)同样您已经找到了布尔运算符有条件的,即&amp;&amp;。尽管当两个陈述都为真时,它们都将评估为真,但这两者并不相同。

      【讨论】:

      • “&&”运算符不应该只在满足两个条件时才返回语句中的某些内容“在这种情况下(同时缺少“.com”和缺少“.info”)时间)?
      • 是的,任何人都无法在不查看更多代码的情况下确认这两个陈述都是正确的。
      • ||不是“或”运算符;它是等效的快捷方式。逻辑(按位)“或”运算符是|
      • @Turing85 谢谢我已经更正了
      • 并非如此。正如我所说:| 是逻辑(按位)“或”,|| 是快捷方式-“或”。
      最近更新 更多