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);
}
from this mrbostons java tutorial:
https://www.thenewboston.com/videos.php?cat=31&video=18001
Though you could (?) write the same code with a if statement, I'd like to know what is going on in this code and how I use it in other places
Here is the equivalent of
hour = ((h>=0 && h < 24) ? h : 0);
with if/elses :
if(h>=0 && h < 24)
hour = h;
else
hour = 0;
The first notation is using a ternary operator.
hour = ((h>=0 && h < 24) ? h : 0);
if h is greater than or equal to zero, and less than 24, then set hour to the value of h, else set hour to zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With