# Comparing 3 or more numbers

• 10-10-2010, 11:23 PM
W00tbeer1
Comparing 3 or more numbers
Hello there,

I'm new to Java and I've recently been wondering how you compare 3 or more numbers that a user inputs.

Basically I referenced a Scanner, and created three int variables which the user inputs. Now how would you display which is the largest and which is the smallest? I know you could make a bunch of if statements, but what would be the most efficient way of doing this?

Thanks.
• 10-10-2010, 11:26 PM
Ronin
Hello W001,

int max = Math.max(a, Math.max(b, c)); // Where a, b, c are the three variable.
int min = Math.min(a, Math.min(b, c));

Regards.
• 10-10-2010, 11:26 PM
Fubarable
Quote:

Originally Posted by W00tbeer1
I'm new to Java and I've recently been wondering how you compare 3 or more numbers that a user inputs.

Basically I referenced a Scanner, and created three int variables which the user inputs. Now how would you display which is the largest and which is the smallest? I know you could make a bunch of if statements, but what would be the most efficient way of doing this?

I think so, yes.

And by the way, welcome to the java-forums.org!
• 10-10-2010, 11:40 PM
W00tbeer1
Quote:

Originally Posted by Ronin
Hello W001,

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

Regards.

Thanks! Now I don't think I am using it correctly, may you please inspect my code and tell me what I'm doing wrong? I'm trying to do the step without creating any extra variables:

Code:

```import java.util.Scanner; public class ReviewProgram {   public static void main(String[] args)   {       Scanner input = new Scanner(System.in);             int num1;       int num2;       int num3;             System.out.print("Enter First Integer: ");       num1 = input.nextInt();       System.out.print("Enter Second Integer: ");       num2 = input.nextInt();       System.out.print("Enter Third Integer: ");       num3 = input.nextInt();             System.out.println();             System.out.printf("Sum: %d\n", (num1 + num2 + num3));       System.out.printf("Average: %d\n", ((num1 + num2 + num3) / 3));       System.out.printf("Product: %d\n", (num1 * num2 * num3));       System.out.printf("Largest Number: %d\n", (Math.max(num1,num2,num3)));       System.out.printf("Smallest Number: %d\n", (Math.min(num1,num2,num3)));         } }```
• 10-10-2010, 11:49 PM
Ronin
You haven't quite followed the example given, Math.max() only takes two parameters whilst you have attempted to use three.
As only two are permitted, then we need to call max() twice, first to compare two numbers, then to compare the third with the larger one found previously.

I refer back to my previous code:

Code:

`Math.max(a, Math.max(b, c));`

See if you can spot it ;).
Once you have solved this, the same applies to finding the min.

Regards.
• 10-10-2010, 11:53 PM
W00tbeer1
Thank you, I have gotten it to work.
It's sorta weird though how you can only enter two integers for that, but I'll get used to it.
• 10-11-2010, 12:02 AM
Ronin
Quote:

Originally Posted by W00tbeer1
It's sorta weird though how you can only enter two integers for that, but I'll get used to it.

Sun had to draw the line somewhere :D.
• 10-11-2010, 05:47 AM
Zack
Quote:

Originally Posted by Ronin
Sun had to draw the line somewhere :D.

Languages like Python and PHP take as many as you want. You could do "min(1,4,5,6,2,1,3,4,1,5,6,1,5,2,1)" if you wanted to. Sun just decided that Java would have tighter standards.
• 10-11-2010, 08:03 AM
DarrylBurke
You can always write your own wrapper method that takes a vararg.
Code:

```public static int max(int... ints) {   int retVal = 0;   for (int i = 0; i < ints.length; i++) {     retVal = Math.max(ints[i], retVal);   }   return retVal; }```
db