Carey Brown wrote:
Hanna Roberts wrote:So you’re saying any time I use nextLine() after a nextInt() I will always have to flush it, so I write it twice?
Yes. This includes nextInt(), nextDouble(), etc., anything OTHER THAN nextLine().
Junilu Lacar wrote:If you're trying to learn about nested if-statements, you wouldn't be using the ternary operator anyway.
Campbell Ritchie wrote:Well done
Hanna 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
Carey Brown wrote:This doesn't make your answer wrong but as go along your Java learning curve you will notice that there are a lot of Java programming conventions. Following the conventions allows seasoned Java programmers to quickly read and understand the intent of the code. When you don't follow conventions it may still work but then the reader has to stop and analyze the code to understand why it was written differently, this can slow the reading process down enormously.
Another convention that popped up in your code is ++i. This is a per-increment instruction. Because there's no other larger expression that it is a part of it behaves the same as i++, a post-increment instruction. The convention is to always use post increment unless you are specifically doing something that requires pre-increment. Again, this is just one of those things you get used to but you'll find your reading and comprehension speed improving.
Carey Brown wrote:
Hanna Roberts wrote:Oh I think I get the problem now. I had the sign backwards it need to be retireAge >= i; right?
Usually the 'i' variable precedes the limit variable. So if you swap it around you get
i <= retireAge