Yes, Math.max and Math.min in a loop saving the result through each iteration. Once the loop is done, you have the max and/or min.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Besides, if you simply want to "take the easy way out", place your numbers in an array (which I assume they are already, anyway), and use Arrays.sort() and then pull the first and last numbers.
;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
for (.....) {
max = Math.max(max, ...);
min ....
}
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Read first number in the array. Set this to be both the highest and lowest initially so we have something to compare against
Then check each subsequent number in the array
Is it higher than the highest number so fat?
- If so then its the new highest number
Is it lower than the lowest number so far?
- If so then its the new lowest number
Loop until you reach the end of the array
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
try this one
import java.util.Arrays;
class max
{
public static void main(String args[])
{
int [] num = {40, 20, 10 ,5, 74, 15, 20,30,95,21};
Arrays.sort(num);
System.out.println("min value"+num[0]);
System.out.println("max value"+num[9]);
}
}
See reply 6.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
No, that wouldn't work. How would it work for a set of numbers of arbitrary size (in other words how would it work when you don't know how many numbers you'll have, since in your example, it only works if you have 3 numbers)? Hard coding it like that would only work if you knew how many numbers you would have. And even then, it is unreadable and overly complex. The suggestions that a few people in here gave will work, but consider that the most efficient solution is probably to keep two variables: one being the lowestValue and one being the highestValue. Then, "look at" each number in your set of numbers once (go through your numbers once) and if it's greater than the highestValue set highestValue to the number you're looking at. And do the same for the lowestValue. If you study Java at all, you will be able to write this method. Nobody is going to hand you the code. But if this is hard to understand I'll give you the psuedocode so you can try to code it on your own:
declare highestVariable and set it to 0
declare lowestVariable and set it to something really high
for each of your numbers, compare the first number to highestVariable. If it's higher than highestVariable, set highestVariable to the current number. Do the equivalent for lowestVariable. Then move onto the second number. Do this for every number in your set of numbers.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
not to mention masijade gave him the entire solution
Well, theform of it anyway. Cut-n-paste code I do not do. ;-)
In either case, I have the feeling that his instructor will want to see a loop, two "save" variables, and two if statements, rather than Math.max, Math.min, or the easiest solution Arrays.sort(). ;-)
Of course, he didn't really state how the instructor worded the exercise, the instructor may even want to see a recursive method. Who knows? ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494