【问题标题】:Conditional operator with INT带 INT 的条件运算符
【发布时间】:2011-10-29 11:53:49
【问题描述】:

如何用条件运算符编写这个 if 语句?

int TopicID = ...
int LastPost = ...

if (LastPost == 0) 
{
    LastPost = TopicID
} else 
{
    LastPost = LastPost;
}

我试过这个:

LastPost == 0 ? LastPost == TopicID : LastPost == LastPost;

但它不起作用,所以你可以看到我不是那么专业..:P

【问题讨论】:

标签: conditional-operator


【解决方案1】:
LastPost = (LastPost == 0 ? TopicID : LastPost);

不管怎样,给自己赋值LastPost是什么意思?只需删除整个 else 块,您将拥有最简单的方法来实现您想要实现的目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-09
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多