Peter Parker
Peter Parker

Reputation: 75

Finding the smallest, largest, and middle values in Java

I did with this code. Is it correct way? I want to sort the numbers in ascending order. Is there better way for this?

import java.lang.Math;
public class Numbers
{
  public static void main(String[] args)
  {
    int a=1;
    int b=2;
    int c=3;

    if (a<b && a<c)
      System.out.println("Smallest: a");
    else if (a>b && a>c)
      System.out.println("Biggest: a");
    else if (a>b && a<c)
      System.out.println("Mid: a");
    else if (a<b && a>c)
      System.out.println("Mid: a");
    if (b<c && b<a)
      System.out.println("Smallest: b");
    else if (b>c && b>a)
      System.out.println("Biggest: b");
    else if (b>c && b<a)
      System.out.println("Mid: b");
    else if (b<c && b>a)
      System.out.println("Mid: b");
    if (c<a && c<b)
      System.out.println("Smallest: c");
    else if (c>a && c>b)
      System.out.println("Biggest: c");
    else if (c>a && c<b)
      System.out.println("Mid: c");
    else if (c<a && c>b)
      System.out.println("Mid: c");
  }
}

Upvotes: 0

Views: 5751

Answers (4)

Supun Sandeeptha
Supun Sandeeptha

Reputation: 165

import java.util.Scanner;

public class SortingIntegers {

    public static void main (String[] args){
         int num1;
         int num2;
         int num3;
         int largerstNum;
         int smallestNum;
         int middleNum;

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the First Integer");
        num1 = sc.nextInt();
        System.out.println("Pleas enter the Second Integer");
        num2 = sc.nextInt();
        System.out.println("Please enter the third Integer");
        num3 = sc.nextInt();

        if (num1 > num2){
            if (num1 > num3){
                largerstNum = num1;
                if (num2 > num3){
                    middleNum = num2;
                    smallestNum = num3;
                }else {
                    middleNum = num3;
                    smallestNum = num2;
                }
            }
        }else {
            if (num1 > num3){
                middleNum = num1;
                if (num2 > num3){
                    largerstNum = num2;
                    smallestNum = num3;
                }else {
                    largerstNum = num3;
                    smallestNum = num2;

                }
            }else {
                smallestNum =num1;
                if (num2 > num3){
                    largerstNum = num2;
                    middleNum = num3;
                }else {
                    largerstNum = num3;
                    middleNum = num2;
                }
            }

            System.out.println("Highest Number is : " + largerstNum);
            System.out.println("Smallest Number is : " + smallestNum);
            System.out.println("Middle Number is : " + middleNum);
        }

    }
}

Upvotes: -1

Dan
Dan

Reputation: 78

In general it would be best to use a loop and a array for this type of thing that way if you have more than 3 numbers it will still work. Also you wont have to type nearly as much. Try something like this for finding the smallest number.

MyArray = new int[3];

MyArray[0] = 1;
MyArray[1] = 2;
MyArray[2] = 3;

int temp = a;

for (int i = 0; i < (number of numbers to check in this case 3); i++){
    if (MyArray[i] < temp){
        temp = MyArray[i];
    }
}

System.out.println("Smallest number is: " + temp);

Upvotes: 1

Konrad H&#246;ffner
Konrad H&#246;ffner

Reputation: 12207

Expanding on Steve's answer (I assume you are new to Java and need a more complete example):

import java.util.Arrays;

public class Numbers
{
  public static void main(String[] args)
  {
    int a=3;
    int b=2;
    int c=1;
    int[] numbers = {a,b,c};
    Arrays.sort(numbers);
    System.out.println("The highest number is "+numbers[2]);
    System.out.println("The middle number is "+numbers[1]);
    System.out.println("The lowest number is "+numbers[0]);
  }
}

Upvotes: 3

Steve
Steve

Reputation: 1615

You can store the three numbers in an array and then do

Arrays.sort(numbers);

/* numbers[0] will contain your minimum
 * numbers[1] will contain the middle value
 * numbers[2] will contain your maximum
 */

That's all!

Upvotes: 2

Related Questions