[英]checking for both true or both false
if I have the following logic to check for both true or both false but ignore the others, I write: 如果我有以下逻辑来检查真或假,但忽略其他,我写:
if (a) {
if (b) {
// do something when both true
}
} else if (!b) {
// do something when both false
}
is that the simplest you can write in java? 这是用Java写的最简单的吗?
of course I can write 我当然可以写
if (a && b) {
// do something when both true
} else if (!a && !b) {
// do something when both false
}
but I am thinking if it is possible to achieve this using only one if condition and else without condition, eg 但是我在想是否有可能仅使用一个if条件而其他条件则没有条件,例如
if (some condition) {
// do something when both true
} else { // <-- check! only else, no more condition
// do something when both false
}
Impossible is of course an acceptable answer to this question (if accompanied with explanation) 当然,不可能是对此问题的可接受答案(如果附有解释)
Thank you 谢谢
Apologies for my initial XOR answer - I had misread your question. 抱歉,我对XOR的最初回答是-您读错了我的问题。 The correct answer is,
正确的答案是
It is impossible in Java.
在Java中是不可能的。
The reason is one of Boolean logic. 原因是布尔逻辑之一。 When you have two logicals, A and B, then there are four possible states:
当您有两个逻辑A和B时,有四个可能的状态:
A B
0 0 << A and B are false
0 1
1 0
1 1 << A and B are true
When you have a condition that says "Both A and B are true", you have just the last state. 当您有一个条件说“ A和B都为真”时,您只有最后一个状态。 Unfortunately, the "else" (all the others) contains three possible states.
不幸的是,“其他”(所有其他)包含三个可能的状态。 You cannot have something that is true for one state, false for another, and "something else" for the other two.
对于一个状态,您不能拥有正确的事物,对于另一种状态,则不能拥有虚假的事物,而对于另外两种状态,则不能拥有“别的事物”。 That's not how Boolean algebra works.
这不是布尔代数的工作方式。
The answer is no . 答案是否定的 。 You can't have an
else
clause that runs only under certain conditions without using an else if
. 您不能拥有仅在特定条件下运行的
else
子句,而不能使用else if
。 An else
clause without else if
will run every time the condition in the if
clause is false. 每当
if
子句中的条件为false时,不else if
的else
子句将运行。 If you want to add additional constraints (such as both a
and b
are false) you need to use an else if
. 如果要添加其他约束(例如
a
和b
均为假),则需要使用else if
。
There is nothing wrong with using else if
though, and it makes your intention fairly clear: 但是,使用
else if
并没有错,这使您的意图相当清楚:
if (a && b) {
} else if (!a && !b) {
}
You have three states you wish to handle differently -- though one case is to "do nothing." 您希望以不同的方式处理三种状态-尽管一种情况是“什么也不做”。
if ( a && b )
{
// Do something when both are true.
}
else if ( ! a && ! b )
{
// Do something if both are false.
}
This is the most economical way to do what you want to achieve -- two tests for three cases. 这是完成您想要实现的目标的最经济的方法-三种情况下进行两次测试。
Alternatively, if you can return 0 or 1 from your "boolean values" or your expressions, you could do something like: 另外,如果您可以从“布尔值”或表达式中返回0或1,则可以执行以下操作:
switch ( a + b )
{
case 0:
// Do something when both a and b are "false."
break;
case 1:
// Do something -- or nothing -- when a or b are "false" and the other is "true."
break;
case 2:
// Do something when both a and b are "true."
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.