【问题标题】:Hey can someone explain me the use of "?" and ":"? [duplicate]嘿,谁能解释一下“?”的用法?和 ”:”? [复制]
【发布时间】:2019-08-26 01:18:55
【问题描述】:

我不知道“?”的用法和“:”。

return value < current.value
                ? containsNodeRecursive(current.left, value)
                : containsNodeRecursive(current.right, value);

【问题讨论】:

标签: java


【解决方案1】:
return value < current.value
            ? containsNodeRecursive(current.left, value)
            : containsNodeRecursive(current.right, value);

等于

if (value < current.value)
    return containsNodeRecursive(current.left, value)
else
    return containsNodeRecursive(current.right, value);

意思是

条件?如果为真则执行:如果为假则执行

【讨论】:

  • 最好不要回答如此明显的重复问题,而是链接到原始问题,提问者会在其中找到更多信息。
【解决方案2】:

完全等价于:

if (value < current.value) {
    return containsNodeRecursive(current.left, value);
else {
    return containsNodeRecursive(current.right, value)
}

只是一种更紧凑的编写方式。通常用于简短的 if/else 条件,不使用五行代码,而只使用一行。

【讨论】:

  • 最好不要回答如此明显的重复问题,而是链接到原始问题,提问者会在其中找到更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 2015-12-21
  • 1970-01-01
相关资源
最近更新 更多