英文:
Boolean operator which gives true from two similar values
问题
给定两个布尔变量x和y,操作符应该是这样的:
boolean getResult(boolean x, boolean y) {
return x op y;
}
以下断言应该通过:
assertEquals(getResult(true, true), true);
assertEquals(getResult(false, false), true);
assertEquals(getResult(true, false), false);
assertEquals(getResult(false, true), false);
是否有适用于此的操作符?
编辑。
抱歉,我忘记提到除了等式检查之外的操作符。我正在寻找一种可以像一般逻辑门中那样给出组合结果的操作。我只想知道是否可能。如果可能,这个操作是什么。
英文:
Given two boolean variables x and y, the operator should be such that
boolean getResult(boolean x, boolean y) {
return x op y;
}
the following assertions pass
assertEquals(getResult(true, true), true);
assertEquals(getResult(false, false), true);
assertEquals(getResult(true, false), false);
assertEquals(getResult(false, true), false);
Is there any operator for this?
Edit.
Sorry, I missed mentioning operator other than equality check. I'm looking for some kind of operation which can give a combined result like we have in general logical gates. I just want to know if its possible or not. If possible, whats the operation.
答案1
得分: 4
是的,可以使用==
操作符来实现这个功能。
请查看下面的解释:
true == true => true
false == false => true
true == false => false
false == true => false
演示:
public class Main {
public static void main(String[] args) {
// 测试
System.out.println(getResult(true, true));
System.out.println(getResult(false, false));
System.out.println(getResult(true, false));
System.out.println(getResult(false, true));
}
static boolean getResult(boolean x, boolean y) {
return x == y;
}
}
输出:
true
true
false
false
或者,您也可以通过取位异或(bitwise exclusive OR)
操作的否定来获得相同的结果,如下所示:
public class Main {
public static void main(String[] args) {
// 测试
System.out.println(getResult(true, true));
System.out.println(getResult(false, false));
System.out.println(getResult(true, false));
System.out.println(getResult(false, true));
}
static boolean getResult(boolean x, boolean y) {
return !(x ^ y);
}
}
输出:
true
true
false
false
英文:
> Is there any operator for this?
Yes, the operator for this can be ==
.
Check the explanation given below:
true == true => true
false == false => true
true == false => false
false == true => false
Demo:
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getResult(true, true));
System.out.println(getResult(false, false));
System.out.println(getResult(true, false));
System.out.println(getResult(false, true));
}
static boolean getResult(boolean x, boolean y) {
return x == y;
}
}
Output:
true
true
false
false
Alternatively, you can get the same result by negating bitwise exclusive OR
operation as demonstrated below:
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getResult(true, true));
System.out.println(getResult(false, false));
System.out.println(getResult(true, false));
System.out.println(getResult(false, true));
}
static boolean getResult(boolean x, boolean y) {
return !(x ^ y);
}
}
Output:
true
true
false
false
答案2
得分: 1
你只需要检查这两个值是否相等。
boolean getResult(boolean x, boolean y) {
return x === y;
}
英文:
You just have to check if the two values are equal.
boolean getResult(boolean x, boolean y) {
return x === y;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论