Results 1 to 9 of 9
  1. #1
    W00tbeer1 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by W00tbeer1 View Post
    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!

  4. #4
    W00tbeer1 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Ronin View Post
    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)));
          
       }
    }

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    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.

  6. #6
    W00tbeer1 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Quote Originally Posted by W00tbeer1 View Post
    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.

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Ronin View Post
    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    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

Similar Threads

  1. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  2. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  3. Problem comparing three numbers
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 02-09-2008, 05:43 PM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM
  5. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 AM

Posting Permissions

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