Hi,
I've solved this one, but I'm not super happy with the method I used. I just did manually wrote comparisons. I'd like to do it with two nested for loops, but I don't see a way to do that using only what we've learned so far. Any thoughts?
Looking for a solution with loops
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Artem Divertitto Senior Android Developer at United Tech
16 April 2020, 08:30
The easiest way is use if else. But at the other hand, you can add three numbers at list, sort it and get number at index 1, but this method you will learn later
+1
Brad Hawk
15 April 2020, 15:50
I guess you could put the numbers into an array, then compare each of them to the others. But you're still having to compare them to every other number. It may save some lines of code, but I think it would be the same number of comparisons.
0
Erik Kristofer Anderson
15 April 2020, 17:03
Thanks. That would work. But I avoided using loops because the course hadn't introduced them yet.
0
Pavlo Plynko Java Developer at CodeGym
16 April 2020, 06:23
So, previously you avoided using loops, but now you would like to do it whith two nested for loops?
So let’s you learn the for loop and do what you intended.
0
Tony Roy
14 April 2020, 22:44
Can you give the task number?
0
Erik Kristofer Anderson
14 April 2020, 22:53
I think it's stated above, but here you go:
---
Question about the task
Somehow average
Java Syntax, Level 4, Lesson 16
---
Also I looked back in IntelliJ, and the directory name was `task0441`.
Thanks and hope that helps!
0
Tony Roy
15 April 2020, 02:25
I did it probably the way you did, by direct comparisons. But you ask an interesting question. What if, say, there were 25 numbers or 1025? Then direct comparisons wouldn't do. One thought is to obtain the complete list, sort, and pick the middle term. I can't remember if you have done sorts yet. You will or have already encountered sorts by nested loops. (If you haven't done it yet, you might check out "bubble sort" on the web -- you could do a mini one on 3 objects. And later you will find java methods that do the same.
+4
Sela
22 June 2020, 14:28
I suggest that for even count of numbers like 1025 do not display anything. for odd count do as Tony Roy suggested i.e.
1. gather the numbers
2. sort them
3. pick the middle of them
0