这一段逻辑是怎么回事?Java,括号

齐通
private int hour;
private int minute;
private int second;

public void setTime(int h, int m, int s){
hour = ((h>=0 && h < 24) ? h : 0);
}

从此mrbostons Java教程中:

https://www.thenewboston.com/videos.php?cat=31&video=18001

尽管您可以使用if语句(?)编写相同的代码,但我想知道这段代码中发生了什么以及如何在其他地方使用它

穆罕默德(Mohammed Aouf Zouag)

这相当于

hour = ((h>=0 && h < 24) ? h : 0);

if/elses:

if(h>=0 && h < 24) 
    hour = h;
else
    hour = 0;

第一种表示法是使用三元运算符

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章