Comparing 3 Integers and Displaying the Largest and smallest

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MBeckford05
    New Member
    • Mar 2007
    • 7

    Comparing 3 Integers and Displaying the Largest and smallest

    Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison operate determine which number is larger and which is smaller. I am new to java I was wondering how to but this into java source code.

    Can anyone Help?

    Thank You.

    MBeckfod05
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Just put your three numbers in an array and use the Arrays.sort() method.
    The number at the start of the array will be the smallest number; the one at the
    end will be the largest number.

    kind regards,

    Jos

    Comment

    • BSCode266
      New Member
      • Jan 2007
      • 38

      #3
      You can also just use if-statements. For 3 integers i would say an array is a bit of an overkill. Just check a couple times which one is the largest and which one is the smallest.

      as an example:
      Code:
      if ( x > y) {
      largest = x;
      }
      You will have to do this a couple times and you are all set.

      BSCode266

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I agree with BSCode. As this sounds like a class assignment type question, it is probably being covered in an intro-level course, and they may not have even gotten to arrays yet!

        Using if...statements is probably your best bet. I would use two 'checks' in each statement - for example, I would want to check if a is bigger than b AND if a is bigger than c.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Ganon11
          I agree with BSCode. As this sounds like a class assignment type question, it is probably being covered in an intro-level course, and they may not have even gotten to arrays yet!

          Using if...statements is probably your best bet. I would use two 'checks' in each statement - for example, I would want to check if a is bigger than b AND if a is bigger than c.
          Don't do that because you'll be throwing away valuable information then. Write it
          out and you'll see that you're basically doing this:
          Code:
          if (a >= b && a >= c) max= a;
          else if (b >= a && b >= c) max = b;
          else if (c >= a && c >= b) max= c;
          ... and then you have to do the same for the minimum value; on top of that
          you'll be testing quite a bit of comparisons twice.

          Better do this then:
          Code:
          if (a >= b) 
             if (a >= c) { max= a; if (b >= c) min= c; else min= b; }
             else { max= c; min= b; }
          else if (b >= c)
             { max= b; if (a >= c) min= c; else min= a; }
          else { max= c; if (a >= b) min= b; else min= a; }
          kind regards,

          Jos (<--- cycle squeezer ;-)

          Comment

          Working...