简体   繁体   English

不明白为什么条件“OR”运算符不适合我?

[英]Don't understand why Conditional “OR” operator not working for me?

Am doing this online exercise and one question is: 我正在做这个在线练习,一个问题是:

We'll say that a number is "teen" if it is in the range 13..19 inclusive. 如果它在13..19(含)范围内,我们会说一个数字是“青少年”。 Given 2 int values, return true if one or the other is teen, but not both 给定2个int值,如果一个或另一个是青少年,则返回true,但不是两者都返回

So I should return true if one of the int values that it passes into my method is in the range of 13 and 19 but return false if both are in the range; 所以我应该返回true,如果它传递给我的方法的一个int值在13和19的范围内,但如果两者都在范围内则返回false; This is the code I wrote: 这是我写的代码:

 public boolean loneTeen(int a, int b) {
  if ( a >= 13 && a <= 19 || b >= 13 && b <= 19 )
  return true;
  else
  return false;
}

basically if integer a is between 13 and 19 inclusive or if integer b is between 13 and 19 inclusive I return true otherwise I return false. 基本上,如果整数a介于13和19之间,或者整数b介于13和19之间,则返回true,否则返回false。 When I test this it works except If I pass in 13 and 13 for integer a and b, I get true instead of false. 当我测试它时它起作用,除非我传入13和13表示整数a和b,我得到的是真而不是假。 My question is I've looked up the conditional "OR(||)" operator and it says that: 我的问题是我查找了条件“OR(||)”运算符,它说:

  • if the first operand determines the overall value for the condition, then the second operand is not evaluated. 如果第一个操作数确定条件的总值,则不计算第二个操作数。

So when I use "Or" operator like my code above doesn't it mean if a is in the range return true or if b is in the range return true but if both are in the range return false? 所以,当我使用“Or”运算符时,如上面的代码并不意味着如果a在范围内返回true或者b是否在范围内返回true但是如果两者都在范围内返回false? what is the differance between using 使用之间的差异是什么?

  • if ( a >= 13 && a <= 19 || b >= 13 && b <= 19 ) and if ( a >= 13 && a <= 19 || b >= 13 && b <= 19 )
  • if ( a >= 13 && a <= 19 && b >= 13 && b <= 19 ) Why isn't it returning false if both values passed in are in the range? if ( a >= 13 && a <= 19 && b >= 13 && b <= 19 )如果传入的两个值都在范围内,为什么不返回false?

Basically all you want is for exactly one of the values isTeen(a) and isTeen(b) to be true, so use XOR : 基本上你想要的只是其中一个isTeen(a)isTeen(b)为真,所以使用XOR

if ((a >= 13 && a <= 19) ^ (b >= 13 && b <= 19))
  • If you replace ^ with || 如果用||替换^ , you allow for the possibility of both a and b being teen numbers. ,你能为目标的可能性, ab是青少年的数字。

  • If you replace ^ with && , you are essentially saying that you always want both a and b to be teen numbers. 如果你用&&替换^ ,你实际上是在说你总是希望ab都是青少年数字。

Neither of these are what you want. 这些都不是你想要的。 XOR, on the other hand, works for your problem perfectly. 另一方面,XOR可以完美地解决您的问题。

The problem is the lack of braces. 问题是缺乏牙箍。

Ideally you would write something like this: 理想情况下你会写这样的东西:

public boolean isPersonTeen(int a) {
  if ( a >= 13 && a <= 19)
      return true;
  // If you came here means the condition failed.
  return false;
}

Now you can just check XOR 现在你可以检查XOR

 isPersonTeen(a) ^ isPersonTeen(b)

Hth! 心连心!

The || || (or) operator returns true if at least one of the parts is true. 如果至少有一个部分为真,则(或)运算符返回true。 Now if both parts are true, is at least one of the parts true? 现在如果两个部分都是真的,那么至少其中一个部分是真的吗? The answer is yes. 答案是肯定的。 That's why when the first part is evaluated as true, it's unnecessary to check the other part. 这就是为什么当第一部分被评估为真时,没有必要检查另一部分。

What you're looking for is the exclusive or (^). 你要找的是独家或(^)。 This operator will return true if only one of the parts is true. 如果只有一个部分为真,则此运算符将返回true。 So, to get the results you want, all you need to do is to replace || 因此,要获得所需的结果,您需要做的就是替换|| for ^, like so: 对于^,像这样:

    public boolean loneTeen(int a, int b) {
        if ( (a >= 13 && a <= 19) ^ (b >= 13 && b <= 19) )
            return true;
        else
            return false;
    }

Edit: Parentheses inside the if statement added after the discussion with David Wallace. 编辑:与David Wallace讨论后添加的if语句中的括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM