I am trying to make a program that finds the max and min of more than 2 inputs and i am trying to use the Math.Min and Math.Max method but i cant seem to get them too work since they only accept 2 parameters.

this is what i tried to use

public static int max(int a, int b, int c);
int max = Math.max(a) Math.max(b) Math.max(c);

can anyone help me try to find the max and min of more than 2 values please. thanks.

You can write a method of your own that, say for example when passed an array of numbers returns the max and the same for min.

I just started programming in Java and i dont think i would know how to program a method to make it detect the min/max in an array. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input?

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.

I just started programming in Java and i dont think i would know how to program a method to make it detect the min/max in an array. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input?

Yes you may new to java, but are you new to numbers and Math too ?
I am not against using Math.min/max but that's a lame excuse. Can you not compare two given numbers for equality/greater than/lesser than condition are you new to that too ?

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.

;-)

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.

How would i make it in a loop?

for (.....) {
  max = Math.max(max, ...);
  min ....
}

thank you so much. :)

How would i make it in a loop?

@masijade : He he, I knew that would be his next question.

@aodpreacher : Well if you feel your question has been answered, you can mark this thread solved and give masijade his deserved and me (somehwhat) undeserved credit ;)

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]);
}
}

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

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.

wouldn't a simple line like

return Math.max( x, Math.max( y, z ) );

work eaiser than an array since math rules state that you have to work from the inside out . Just a question, I to am new to this and am on here looking for ideas and answers for my own homework...

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.

declare highestVariable and set it to 0
declare lowestVariable and set it to something really high

@BestJewSinceJC : This is not a good idea. Say for example in the first case all the nnumbers in the array are negative. what then ? Your approach would certainly yield the wrong answer. Also in the second case, would you be able to define a really high value ? Also what if all the numbers are greater than that ?

@OP : For this one should set the min and max to the very first numbers from the array and then compare with all the other numbers, modifying the min wherever a lower number is found and modifying the max wherever a greater number is found. In the end min and max have the answers to your question.

PS : I am surprised this thread should be lasting more than 4-5 posts. I think we all have given the OP sufficient material for thought (not to mention masijade gave him the entire solution) and further posts should be submitted after the OP provides proof of effort from his side.

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

my programming PhD always taught me to make my own methods,
you can fit them to what you want, and that's really a good practice for programming.

try to make that algorithm, min/max is one of the easiest when you begin to work it out.

not to mention masijade gave him the entire solution

Well, the form 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? ;-)

i think you must try this
method for find max and the same method for min
private int findMax(int a, int b, int c)
{int max=math.max(Math.max(a,b),c)
return max;
}

i think you must try this
method for find max and the same method for min
private int findMax(int a, int b, int c)
{int max=math.max(Math.max(a,b),c)
return max;
}

Have you even taken care to understand what his problem domain specifies ? He clearly mentions that there can be more than 2 nos so it means there can be more than 3 nos too. How does your program work when there are more than 3 numbers ? Will you be able to explain that ? This approach has already been suggested and somebody has already mentioned that this hardcoded approach won't work.
While suggesting solutions remember to read the problem domain correctly, understand it and also don't forget to read the solutions offered by others.

Yes you may new to java, but are you new to numbers and Math too ?
I am not against using Math.min/max but that's a lame excuse. Can you not compare two given numbers for equality/greater than/lesser than condition are you new to that too ?

I wonder why Java provides methods to do just what he asked...without all the "cool" code for the supposed "big brains". Come out of your tree and next time maybe just give a "simple answer.

OBVIOUSLY simple is always better IF IT SOLVES THE PROBLEM.....

phd smeehd... my 20 years of coding in the real word taught me the following important tidbits are the key to success
1) Think outside the box (you'll shine because the world exists in the box)
2) The shortest way from a to b is a straight line (do not over engineer just to stroke yourself, as you'll endup being the only person stroking you)
3) Take the time to write bugless code (not through unit testing but through extreme desk checking, BEEEE the computer) write once non fix required code is by far the most efficient business practice... YES, this is possible (if you suffer borderline OCD).

THAT said, after 10 seconds of thought I would have gone with the compare loop set min value on comparison etc... etc... HOWEVER surely not the most efficient way... I think the array sort approach is tres clever.

thats my 10 cents worth.

Frank
ZiZi Software

however, just because the code you see look simple doesnt mean it's efficient... how much processing does the array.sort do under the covers....
was this a trick question
;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.