如果任何和所有布尔值都为 True,则返回 True。去掉假的

Lon*_*nzo -2 c# logic

我有三个布尔值,在退出方法之前无法弄清楚如何在每个真正的代码中执行代码。如果我使用 if() else if() else ()它就行不通,因为它仍然只会执行其中之一。我不知道怎么做。基本上我想要做的是在继续之前语句变为真的任何和所有 if 语句中执行代码。

例子:

如果 bools 2 & 3 为真,但 1 不是,那么我首先想在返回 true 之前执行 bools 2 & 3 的 if 语句中的代码。

如果 bools 1 & 3 为真,但 2 不是,那么我首先想在返回 true 之前执行 bools 1 & 2 的 if 语句中的代码。

代码如下:

            if (bool1)
            {
                Execute My Code part 1
                return true;
            }
            else
            {
                return false;
            }

            if (bool2)
            {
                Execute My Code part 2
                return true;
            }
            else
            {
                return false;
            }

            if (bool3)
            {
                Execute My Code part 3
                return true;
            }
            else
            {
                return false;
            }
Run Code Online (Sandbox Code Playgroud)

Pey*_*avi 5

另一个尝试:

if (bool1)
    execute_bool1();
if (bool2)
    execute_bool2();
if (bool3)
    execute_bool3();

return (bool1 || bool2 || bool3);
Run Code Online (Sandbox Code Playgroud)