【问题标题】:Code conversion to ternary operator代码转换为三元运算符
【发布时间】:2014-09-16 16:37:23
【问题描述】:

如何将下面的逻辑转换为三元 (?:) 运算符?

if (node.getLeftChild() == null)
    return 0;
else
    return node.getLeftChild().getValue();

【问题讨论】:

  • return (expression) ? (true-case-value) : (false-case-value);
  • 寻找解决方案也会对您有所帮助!仔细阅读stackoverflow.com/tour
  • 信息点可帮助您搜索此内容...您正在寻找的是“运算符”而不是“函数”,虽然它是 a三元运算符(甚至可能是语言中唯一的一个)它不是 the 三元运算符(因为可能还有更多)。它是“条件运算符”。

标签: java ternary-operator


【解决方案1】:
return (node.getLeftChild() == null) ?  0 : node.getLeftChild().getValue();

?:的用法如下:

condition ? value_if_true : value_if_false 

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2021-02-22
    • 1970-01-01
    • 2015-07-13
    • 2018-08-20
    • 2021-03-17
    • 2015-02-07
    • 2017-08-26
    相关资源
    最近更新 更多