This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You're comparing two adjacent items from the list, and based on that comparison, you set the greatest variable. You need to compare the greatest variable to all items in the list. I'm not going to show you the correct code, try to figure it out yourself.
It sometimes accidentally seems to work. It depends on the ordering of the elements in your list. As a practice, you can try to find out and formulate the conditions which lead to the code actually returning the largest value.
The least() function has the same problem. Fixing it will be very similar to fixing this.
Let's say you want to comment out a piece of code that already contains a block comment somewhere within it. If you use another block comment to comment it out, it will break because the terminating */ of the "inner" comment will terminate the "outer" comment.
Line comments avoid this problem. You'll make the job of another programmer maintaining your code much easier if you use line comments.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Oct 13, 2005
Agree. The different sort of comments all have their place, and they are all suitable for different purposes. The line-end comment, applied at the start of a line, is immune from */ appearing anywhere in that line. Some editors and IDEs will automatically comment-out or comment-in several lines if you highlight them and push a certain combination of keys, possibly ctrl-shft-C.