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语句(?)编写相同的代码,但我想知道这段代码中发生了什么以及如何在其他地方使用它
这相当于
hour = ((h>=0 && h < 24) ? h : 0);
与if/else
s:
if(h>=0 && h < 24)
hour = h;
else
hour = 0;
第一种表示法是使用三元运算符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句