skip to Main Content

JAVA: SORT THREE NUMBERS

JAVA: SORT THREE NUMBERS

JAVA: SORT THREE NUMBERS

Java: Sort three numbers

package chapter6_6_5; import java.util.Scanner; /** * * @author Siavash Bakhshi */ public class Chapter6_6_5 { /** * @param args the command line arguments */ /** Main method */ public static void main(String[] args) { double num1; double num2; double num3; // Create a Scanner Scanner input = new Scanner(System.in); System.out.print("Please enter 1st Number: "); num1 = input.nextDouble(); System.out.print("Please enter 2nd Number: "); num2 = input.nextDouble(); System.out.print("Please enter 3rd Number: "); num3 = input.nextDouble(); displaySortedNumbers(num1, num2, num3); } /** Return the max between two numbers */ public static void displaySortedNumbers(double num1, double num2, double num3) { double max=0; double med=0; double min=0; //get Max if ((num1 > num2 && num1 > num3)) { max = num1; if(num2 > num3) { med = num2; min = num3; } else { med = num3; min = num2; } } else if ((num2 > num1 && num2 > num3)) { max = num2; if(num3 > num1) { med = num3; min = num1; } else { med = num1; min = num3; } } else if ((num3 > num1 && num3 > num2)) { max = num3; if(num1 > num2) { med = num1; min = num2; } else { med = num2; min = num1; } } else { System.out.println("ERROR!"); } System.out.println("Numbers in increasing order: " + min + " , " + med + " , " + max); } }