gpt4 book ai didi

php - 如何改进 if 算法

转载 作者:可可西里 更新时间:2023-10-31 22:42:34 26 4
gpt4 key购买 nike

这个怎么做比较好?无需重复代码 someFunction(1)。

        if(someTrueOrFalse)
{
if(OthersomeTrueOrFalse)
{
someFunction(1);
}
}
else
{
someFunction(1);
}

最佳答案

A = someTrueOrFalse 和 B = OthersomeTrueOrFalse

 A | B | outcome
-----------------
0 | 0 | 1
0 | 1 | 1
1 | 0 | 0
1 | 1 | 1

因此:

if (!(someTrueOrFalse && !OthersomeTrueOrFalse)) {
someFunction(1);
}

或者,等同于@axiac 的评论

if (!someTrueOrFalse || OthersomeTrueOrFalse) {
someFunction(1);
}

我猜这两者中哪一个看起来更好取决于情况(或者有时这只是一个品味问题)。

关于php - 如何改进 if 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28832291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com