gpt4 book ai didi

java - java代码中的问号

转载 作者:行者123 更新时间:2023-12-01 06:55:46 31 4
gpt4 key购买 nike

有人能解释一下下面代码中的问号吗? INITIAL_PERMANCE 也是代码中的静态最终常量,但是语法的最后一行被称为什么?

Synapse(AbstractCell inputSource, float permanence) {
_inputSource = inputSource;
_permanence = permanence==0.0 ?
INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
}

最佳答案

那个?和 : 是 java 条件运算符的一部分。有时称为三元运算符,因为它是 Java 中唯一接受 3 个参数的运算符。

这本质上是一个内联 IF/THEN/ELSE block 。

_permanence = permanence==0.0 ? 
INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);

可以重写如下:

if (permanence == 0.0)
_permanence = INITIAL_PERMANENCE;
else
_permanence = (float) Math.min(1.0,permanence);

条件运算符的一般形式为

<Test returning a boolean> ? <value for if test is true> : <value for if test is false>

关于java - java代码中的问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434021/

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