Sorting Three Numbers

04 November 2011
Write the following method to display three numbers in decreasing order:

public static void displaySortedNumbers(double num1, double num2, double num3)


Input:
5678 1234 6789

Output:
6789.0
5678.0
1234.0

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sortingthreenumbers;

import java.util.Scanner;

/**
 *
 * @author dekwan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        double num1 = scan.nextDouble();
        double num2 = scan.nextDouble();
        double num3 = scan.nextDouble();

        displaySortedNumbers(num1, num2, num3);
    }

    public static void displaySortedNumbers(double num1, double num2, double num3)
    {
        double temp;

        while (num1 < num2 || num2 < num3)
        {
            temp = num1;
            num1 = num2;
            num2 = temp;

            while (num2 < num3)
            {
                temp = num2;
                num2 = num3;
                num3 = temp;
            }
        }

        sopl("");
        sopl("" + num1);
        sopl("" + num2);
        sopl("" + num3);
    }

    public static void sopl(String str)
    {
        System.out.println(str);
    }

}



0 comments:

Catat Ulasan

Terima kasih kerana memberi ulasan...