I
I
Igor Malyga2016-08-12 01:15:45
Java
Igor Malyga, 2016-08-12 01:15:45

How to handle more than two numbers in Math.max method?

I started learning Java, Horstmann's assignment requires using the Math.max method to determine the largest of the 3 entered numbers, but as far as I understand, the method can only compare 2 numbers, or have I misunderstood something?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanan Yuzb, 2016-08-12
@SmallMiracle

Yes, you are right, but it takes two arguments, but the task is done differently. int max = Math.max(Math.max(a,b),c);those. there is a maximum of two numbers, and then we find the largest among the maximum and the third number.

E
Evhen, 2016-08-12
@EugeneP2

1st option

TreeSet<Double> doubles = new TreeSet<>(Arrays.asList(1.34, 2.34, 3.34, 1.74, 0.34));
    System.out.println(doubles.last());

2nd option
Use commons-lang3 library, utility class org.apache.commons.lang3.math.NumberUtils
double max = NumberUtils.max(1.34, 2.34, 3.34, 1.74, 0.34);
    System.out.println(max);

3.34

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question