Java 优雅的分支(任何语言)
我只是编程了一小段时间,还没有找到一种方法来编写分支代码,以覆盖两个或多个布尔值的所有可能配置,而不需要任何冗余的if语句 例如,显示所有可能结果的表格可能如下所示:Java 优雅的分支(任何语言),java,syntax,branch,conventions,Java,Syntax,Branch,Conventions,我只是编程了一小段时间,还没有找到一种方法来编写分支代码,以覆盖两个或多个布尔值的所有可能配置,而不需要任何冗余的if语句 例如,显示所有可能结果的表格可能如下所示: true false --------------------- true | Thing A | Thing B | --------------------- false | Thing C | Thing D | --------------------- 然
true false
---------------------
true | Thing A | Thing B |
---------------------
false | Thing C | Thing D |
---------------------
然而,我能想到的编程东西A到D的唯一代码如下:
if(boolean1) {
if(boolean2)
ThingA();
else
ThingB();
}
else if (boolean2)
ThingC();
else
ThingD();
有没有办法不用写两次布尔2的支票就写这个?我知道这段代码的一次迭代只能运行一次检查,但在需要担心两个以上布尔值的情况下,这种方法可能会变得相当麻烦。如果您的目标是消除If语句,那么函数指针的4项查找表将是您最优雅的答案;将两位(布尔值)组合成一个2位值,并使用该值确定要使用的查找表条目,即在伪代码中:
static FunctionArrayInYourLanguage fnAr[4] {ThingA(), ThingB(), ThingC(), ThingD()}
UnsignedIntegerType manyBooleans = (boolean1 LeftShift 1) BitwiseOR boolean2
Remark: Or any other way of combining your booleans, like casting a struct of 8 bits as a uint8_t in C or vice versa
Execute fnAr[manyBooleans]();
请注意,如果您能够提供查找表空间,那么这种技术可以扩展到多个布尔值。如果您确实为每个可能的组合提供了一个函数,那么您肯定能够提供足够的空间。您可以做的是使用位移位创建一个掩码并引入一个开关
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdint>
using namespace std;
int main() {
bool bArray[2];
for (size_t i = 0 ; i < 2 ; i++) {
cin >> bArray[i];
}
uint32_t mask = 0;
mask = bArray[0];
mask = mask << 1;
mask = mask ^ bArray[1];
switch(mask) {
case 0:
cout << "bot false" << endl;
break;
case 1:
cout << "first false, second true" << endl;
break;
case 2:
cout << "first true, second false" << endl;
break;
case 3:
cout << "both true" << endl;
break;
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
布尔·巴里[2];
对于(大小i=0;i<2;i++){
中国>>巴拉伊[i];
}
uint32_t mask=0;
mask=bArray[0];
mask=mask有一种面向对象的方法
public interface Branch {
public boolean isCondition();
public void execute();
}
public class FirstThing implements Branch {
private boolean boolean1;
private boolean boolean2;
public FirstThing(boolean boolean1, boolean boolean2) {
this.boolean1 = boolean1;
this.boolean2 = boolean2;
}
@Override
public boolean isCondition() {
return boolean1 & boolean2;
}
@Override
public void execute() {
thing00();
}
}
对于表的其余条目,依此类推
将每个实现类的一个实例添加到列表中。在列表中循环,直到isCondition()为true,然后执行().如果你有两个以上的条件,不管怎样,这都会非常麻烦和难看。我建议使用查找表作为一种更优雅的方法。谢谢你,奥利!快速的谷歌告诉我,查找表几乎可以完全满足我的要求。然而,据我所知,isCondition()方法只能返回两个可能的值-true和false。按位and无法区分“true和false”与“false和false和true”。这种方法是否意味着列表中第一个具有false isCondition值的分支对象将被选择用于四个可能结果中的三个?+1,因为此解决方案易读且易于扩展(最多32个布尔值,这比我所需要的还要多)。对于这个场景,switch case也比if-else语句更快,这是我喜欢的。
public interface Branch {
public boolean isCondition();
public void execute();
}
public class FirstThing implements Branch {
private boolean boolean1;
private boolean boolean2;
public FirstThing(boolean boolean1, boolean boolean2) {
this.boolean1 = boolean1;
this.boolean2 = boolean2;
}
@Override
public boolean isCondition() {
return boolean1 & boolean2;
}
@Override
public void execute() {
thing00();
}
}