有没有简单的方法来代码中的真值表?它有2个输入和4个结果,如下所示:
我目前的代码是:
private void myMethod(bool param1, bool param2)
{
Func<int, int, bool> myFunc;
if (param1)
{
if (param2)
myFunc = (x, y) => x >= y;
else
myFunc = (x, y) => x <= y;
}
else
{
if (param2)
myFunc = (x, y) => x < y;
else
myFunc = (x, y) => x > y;
}
//do more stuff
}
Run Code Online (Sandbox Code Playgroud)
我建议使用数组,即
// XOR truth table
bool[][] truthTable = new bool[][] {
new bool[] {false, true},
new bool[] {true, false},
};
Run Code Online (Sandbox Code Playgroud)
...
private void myMethod(bool param1, bool param2, bool[][] table) {
return table[param1 ? 0 : 1][param2 ? 0 : 1];
}
Run Code Online (Sandbox Code Playgroud)