Results 1 to 9 of 9
Thread: Comparing 3 or more numbers
 10102010, 10:23 PM #1Member
 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.
 10102010, 10:26 PM #2Senior Member
 Join Date
 Oct 2010
 Posts
 384
 Rep Power
 5
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; 10102010 at 10:33 PM. Reason: Comment

 10102010, 10:40 PM #4Member
 Join Date
 Sep 2010
 Posts
 3
 Rep Power
 0
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))); } }
 10102010, 10:49 PM #5Senior Member
 Join Date
 Oct 2010
 Posts
 384
 Rep Power
 5
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.
 10102010, 10:53 PM #6Member
 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.
 10102010, 11:02 PM #7Senior Member
 Join Date
 Oct 2010
 Posts
 384
 Rep Power
 5
 10112010, 04:47 AM #8
 10112010, 07:03 AM #9
Similar Threads

printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05212008, 09:03 AM 
Problem comparing three numbers
By gammaman in forum New To JavaReplies: 1Last Post: 02092008, 05:43 PM 
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12032007, 09:44 AM 
comparing
By Feng in forum New To JavaReplies: 2Last Post: 11232007, 09:40 AM
Bookmarks