public static void displaySortedNumbers(double num1, double num2, double num 3)
Write a test program that prompts the user to enter three numbers and invokes the method to display them in decreasing order.
import java.util.Scanner; public class C1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請輸入3個數字:"); double n1 = input.nextDouble(); double n2 = input.nextDouble(); double n3 = input.nextDouble(); displaySortedNumbers(n1,n2,n3); } public static void displaySortedNumbers(double num1 ,double num2 ,double num3 ) { //比較並印出結果 if(num1 > num2) { if( num1 >= num3) { if(num2 >= num3 ) { System.out.println(num1+ " " + num2 + " " +num3); } else if(num3 >= num2 ) { System.out.println(num1+ " " + num3 + " " +num2); } } } else if(num2 > num3) { if( num2 >= num1) { if(num3 >= num1 ) { System.out.println(num2+ " " + num3 + " " +num1); } else if(num1 >= num3 ) { System.out.println(num2+ " " + num1 + " " +num3); } } } else if(num3 > num1) { if( num3 >= num2) { if(num1 >= num2 ) { System.out.println(num3+ " " + num1 + " " +num2); } else if(num2 >= num1 ) { System.out.println(num3+ " " + num2 + " " +num1); } } } else if(num1 == num2 && num2 == num3) { System.out.println(num1+ " " + num2 + " " +num3); } } }
沒有留言:
張貼留言