使用 java 运算符(XOR 和 AND/OR)

发布于 2024-10-12 12:18:48 字数 829 浏览 4 评论 0原文

在程序中,我试图检查两个布尔值(从函数返回);需要检查的条件是:
-只有当返回值中的任何一个为 true 而另一个为 false 时,我才会遇到问题;
-否则,如果两者都是真或假,我很高兴进入下一步。

以下两个示例中哪一个是检查条件的有效方法,或者是否有更好的解决方案?
a 和 b 是整数值,我正在检查 isCorrect 函数中的正确性条件,它返回 true 或 false。

1.2

 // checking for the correctness of both a and b
 if ((isCorrect(a) && !isCorrect(b)) ||
     (!isCorrect(a) && isCorrect(b)))
 { 
   // a OR b is incorrect
 }
 else
 {
   // a AND b are both correct or incorrect
 }

.

  // checking for the correctness of both a and b
  if (! (isCorrect(a) ^ isCorrect(b)))
  {
    // a OR b is incorrect
  }
  else
  {
    // a AND b are correct or incorrect
  }


谢谢,
Ivar

P.S:代码可读性不是问题。 编辑:我的意思是在第二个选项中有一个异或。 另外,我同意 == 和 != 选项,但是如果我必须使用布尔运算符怎么办?

In a program I am trying to check two boolean values (returned from a function); the condition that needs to be checked is:

- only if any one of the returned value is true and the other is false then I have a problem;

- else if both are true or false I am good to go to next step.

Which of the following two examples would be the efficient way to check the condition, or is there a better solution?

a and b are integer values on which I am checking a condition for correctness in isCorrect function and it return true or false.

1.

 // checking for the correctness of both a and b
 if ((isCorrect(a) && !isCorrect(b)) ||
     (!isCorrect(a) && isCorrect(b)))
 { 
   // a OR b is incorrect
 }
 else
 {
   // a AND b are both correct or incorrect
 }

2.

  // checking for the correctness of both a and b
  if (! (isCorrect(a) ^ isCorrect(b)))
  {
    // a OR b is incorrect
  }
  else
  {
    // a AND b are correct or incorrect
  }

Thanks,

Ivar

P.S: code readability is not an issue.
EDIT: I meant to have an XOR in the second option.
Also, I agree with the == and != options, but what if I had to use boolean operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飞烟轻若梦 2024-10-19 12:18:48
if (isCorrect(a) != isCorrect(b)) {
  // a OR b is incorrect
} else {
  // a AND b are correct or incorrect
}
if (isCorrect(a) != isCorrect(b)) {
  // a OR b is incorrect
} else {
  // a AND b are correct or incorrect
}
奈何桥上唱咆哮 2024-10-19 12:18:48

您的测试不需要布尔运算符,只需这样:

if (isCorrect(a) == isCorrect(b)) {
    // they both have the same value
} else {
    // they don't ...
}

编辑 - 我故意不使用相同的注释来反映注释的主要目的应该是描述意图,而不是具体的实现。在这种情况下,最简单的意图声明是 ab 获得相同的结果。

Your test doesn't need boolean operators, just this:

if (isCorrect(a) == isCorrect(b)) {
    // they both have the same value
} else {
    // they don't ...
}

EDIT - I deliberately didn't use the same comments to reflect that the primary purpose of the comment should be to describe the intent, and not the specific implementation. In this case the simplest statement of intent is that both a and b obtained the same result.

↘人皮目录ツ 2024-10-19 12:18:48

简单地:

  if (isCorrect(a) == isCorrect(b))
  {
       // a AND b are both correct or incorrect
  } else {
      // a OR b is incorrect

  }

simply:

  if (isCorrect(a) == isCorrect(b))
  {
       // a AND b are both correct or incorrect
  } else {
      // a OR b is incorrect

  }
贱人配狗天长地久 2024-10-19 12:18:48

这个怎么样?

if(isCorrect(a) != isCorrect(b))
{
  //problem
}
else
{
  //not a problem
}

您也可以使用 XOR,但是 != 工作正常,并且如果您处理布尔值,则更具可读性,IMO。

How about this?

if(isCorrect(a) != isCorrect(b))
{
  //problem
}
else
{
  //not a problem
}

You can use XOR also, but != works fine and is more readable if you are dealing with boolean values, IMO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文