Thread: Comparing 3 or more numbers

1. Member Join Date
Sep 2010
Posts
3
Rep Power
0 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.  Reply With Quote

2. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10 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.
Last edited by Ronin; 10-10-2010 at 10:33 PM. Reason: Comment  Reply With Quote

3.  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!  Reply With Quote

4. Member Join Date
Sep 2010
Posts
3
Rep Power
0  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:

Java 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)));

}
}  Reply With Quote

5. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10 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:

Java 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.  Reply With Quote

6. Member Join Date
Sep 2010
Posts
3
Rep Power
0 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.  Reply With Quote

7. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10  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.  Reply With Quote

8.  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.  Reply With Quote

9. You can always write your own wrapper method that takes a vararg.
Java 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  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•