針對這個題目,我就簡單的使用兩個方法進行排序:
方法一:
public class TreeOfSort{ public static void main(String [] args){ //由鍵盤輸入三個數字 Scanner input = new Scanner(System.in); int [] a = new int[3]; for(int i = 0; i < 3; i++) { a[i] = input.nextInt(); } //調用內部函數 Arrays.sort(a); for(int j = 0; j<3; j++) { System.out.print(a[j] + "\t"); } } }
方法二:
package studying; import java.util.Scanner; /* * This method is not recommended! * It is easy to make a mistake by comparing it abruptly. */ public class ThreeOfSort2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("please enter the first number:"); int a = input.nextInt(); System.out.println("please enter the second number:"); int b = input.nextInt(); System.out.println("please enter the third number:"); int c = input.nextInt(); if(a > b) { if(c > a) { System.out.println(b + "," + a + "," + c); }else if(c < b) { System.out.println(c + "," + b + "," + a); }else { System.out.println(b + "," + c + "," + a); } }else {//a < b if(c < a) { System.out.println(c + "," + a + "," + b); }else if(c > b) { System.out.println(a + "," + b + "," + c); }else { System.out.println(a + "," + c + "," + b); } } } }