I got quite a large code with 4 different conditions which I tried to shorten using the conditional ternary operator as descibed here. However, I can't manage the right syntax since I have more than 2 conditions. Could someone explain how to use the ternary operator in such case? My code goes below
And no, I'm not asking to write code for me, I'm looking for an explanation of ternary operator use with multiple conditions
if (mp.getCurrentPosition() / 1000 / 60 < 10
&& mp.getCurrentPosition() / 1000 % 60 < 10) {
tvTimeElapsed.setText("0"
+ Integer.toString(mp.getCurrentPosition() / 1000 / 60)
+ ":" + "0"
+ Integer.toString(mp.getCurrentPosition() / 1000 % 60));
} else if (mp.getCurrentPosition() / 1000 / 60 < 10
&& mp.getCurrentPosition() / 1000 % 60 >= 10) {
tvTimeElapsed.setText("0"
+ Integer.toString(mp.getCurrentPosition() / 1000 / 60)
+ ":"
+ Integer.toString(mp.getCurrentPosition() / 1000 % 60));
} else if (mp.getCurrentPosition() / 1000 / 60 >= 10
&& mp.getCurrentPosition() / 1000 % 60 < 10) {
tvTimeElapsed
.setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
+ ":"
+ "0"
+ Integer.toString(mp.getCurrentPosition() / 1000 % 60));
} else {
tvTimeElapsed
.setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
+ ":"
+ Integer.toString(mp.getCurrentPosition() / 1000 % 60));
}
?:just results in code that's hard to read.a ? b : (c ? d : (e ? f : g))