Carey Brown wrote:The problem with this is that
requires an &&
Junilu Lacar wrote:
Carey Brown wrote:The problem with this is that
requires an &&
Why is that a problem? It's effectively the same as having another nested if statement.
Carey Brown wrote:but that seems to be Hanna's problem, she jumps to && instead of nesting.
Well doneHanna Roberts wrote:. . . it worked out well . . .
Remember that ?: is equivalent to an if‑else squashed into a single expression, and ?: expressions can be nested inside one another. Did you have a stipulation that the three values be different? A Scanner will accept an int with nextDouble() and convert an input like 123 to 123.0.jshell> int max(int i, int j, int k)
...> {
...> return i > j ?
...> i > k ? i : k :
...> j > k ? j : k;
...> }
 created method max(int,int,int)
jshell> max(123, 234, 345)
$5 ==> 345
jshell> max(123, 345, 234)
$6 ==> 345
jshell> max(345, 234 ,123)
$7 ==> 345
jshell> int min(int i, int j, int k)
...> {
...> return i < j ?
...> i < k ? i : k :
...> j < k ? j : k ;
...> }
 created method min(int,int,int)
jshell> min(345, 234 ,123)
$9 ==> 123
jshell> min(345, 123, 234)
$10 ==> 123
jshell> min(123, 345, 234)
$11 ==> 123
Campbell Ritchie wrote:
Well doneHanna Roberts wrote:. . . it worked out well . . .
I would have used the ?: operator myself. It is easy enough to find the largest and smallest of three, which I did on JShell, but ran out of time and energy enough to work out the middle value.Remember that ?: is equivalent to an if‑else squashed into a single expression, and ?: expressions can be nested inside one another. Did you have a stipulation that the three values be different? A Scanner will accept an int with nextDouble() and convert an input like 123 to 123.0.jshell> int max(int i, int j, int k)
...> {
...> return i > j ?
...> i > k ? i : k :
...> j > k ? j : k;
...> }
 created method max(int,int,int)
jshell> max(123, 234, 345)
$5 ==> 345
jshell> max(123, 345, 234)
$6 ==> 345
jshell> max(345, 234 ,123)
$7 ==> 345
jshell> int min(int i, int j, int k)
...> {
...> return i < j ?
...> i < k ? i : k :
...> j < k ? j : k ;
...> }
 created method min(int,int,int)
jshell> min(345, 234 ,123)
$9 ==> 123
jshell> min(345, 123, 234)
$10 ==> 123
jshell> min(123, 345, 234)
$11 ==> 123
Junilu Lacar wrote:If you're trying to learn about nested ifstatements, you wouldn't be using the ternary operator anyway.
That's a pleasureHanna Roberts wrote:. . . Thanks . . .
Apart from the fact that expressions with multiple ?: can be quite illegible, I think many beginners are scared of ?: and ought to try it so they can see how it works, and how useful it can be. Stick to only one ?: per statement at first.I don't think I've learned that operator yet . . . .
Koen Ursem wrote:In the case && would not be allowed i would asume this could do as well?
Carey Brown wrote:The structure I would have preferred
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filleradvertising
