// 導包 import java.util.Scanner; public class Sort { public static void main(String[] args) { // 創建鍵盤輸入對象 Scanner sc = new Scanner(System.out.println); ...
1 import java.util.Scanner;
2
3 public class Test {
4 public static void main(String[] args) {
5 // 創建鍵盤輸入對象
6 Scanner input = new Scanner(System.in);
7
8 // 接收用戶輸入的三個整數
9 System.out.print("請輸入第一個整數:");
10 int num1 = input.nextInt();
11 System.out.print("請輸入第二個整數:");
12 int num2 = input.nextInt();
13 System.out.print("請輸入第三個整數:");
14 int num3 = input.nextInt();
15
16 // 排序
17 if (num1 > num2) {
18 int temp = num1;
19 num1 = num2;
20 num2 = temp;
21 }
22
23 if (num1 > num3) {
24 int temp = num1;
25 num1 = num3;
26 num3 = temp;
27 }
28
29 if (num2 > num3) {
30 int temp = num2;
31 num2 = num3;
32 num3 = temp;
33 }
34
35 // 輸出
36 System.out.println("三個整數從小到大排列:" + num1 + " " + num2 + " " + num3);
37 }
38 }