C#:根据 bool 值选择运算符(一行)

标签 c# operators

我正在尝试使用光滑的单衬垫,因为我觉得这可能是可能的。

我将把我的代码放在下面,然后尝试更多地解释我想要实现的目标。

        for (int p = 0; p < 2; p++)
        {
            foreach (string player in players[p])
            {
                if (PlayerSkills[player].streak_count *>* 0) //This line
                    PlayerSkills[player].streak_count++;
                else
                    PlayerSkills[player].streak_count = 0;
            }
        }

*(p==0 ? >:<) 根据 p 选择比较运算符。

当然我写的是垃圾。但基本上我想在 p==0 时使用 >0,在 p>>0 时使用 <0。有实现此目标的好方法吗?

最佳答案

好吧,您应该使用最易读的内容,即使它不是那么合理。也就是说……

// Invert the count for all but the first player and check for a positive number
if (PlayerSkills[player].streak_count * (p==0 ? 1 : -1) > 0)

关于C#:根据 bool 值选择运算符(一行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24191789/

相关文章:

c# - asp.net core 2.0区分过期session和新用户

prolog - 在 SWI-prolog 中定义包含竖线 (|) 的运算符

python peewee 文档 : example app: unknown syntax/feature

c# - 如何在日期时间中添加月份并与 Linq 中的另一个日期时间进行比较?

ruby - 在 Ruby 中,为什么 && 在 || 时工作?不在我的示例代码中?

java - 小于运算符也评估相等值

c - (int ret = 0; !ret && ret !=0) 之间的区别

c# - C# 中的字符串操作以递增数字

c# - 如何通过 .NET/C# 查找 CPU 核心数?

c# - .NET xsd 导入器创建不可序列化的类