这段逻辑是怎么回事? Java, 括号
What is going on in this piece of logic ? Java, parenthesis
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;
第一个表示法使用了三元运算符。
hour = ((h>=0 && h < 24) ? h : 0);
如果 h 大于或等于零且小于 24,则将 hour
设置为 h
的值,否则将小时设置为零。
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;
第一个表示法使用了三元运算符。
hour = ((h>=0 && h < 24) ? h : 0);
如果 h 大于或等于零且小于 24,则将 hour
设置为 h
的值,否则将小时设置为零。