[20-04-28][Self-test 17]Java SortThreeNum

 1 package test_3_1;
 2 
 3 import java.util.Scanner;
 4 
 5 public class SortThreeNum {
 6 
 7     public static void main(String[] args) {
 8         
 9         /** 输入三个整数x,y,z,请把这三个数由小到大输出 */
10         Scanner sc = new Scanner(System.in);
11         System.out.println("请输入第一个整数");
12         int x = sc.nextInt();
13         System.out.println("请输入第二个整数");
14         int y = sc.nextInt();
15         System.out.println("请输入第三个整数");
16         int z = sc.nextInt();
17         
18         sort(x, y, z);
19         
20 
21     }
22 
23     /**
24      * 排序
25      * 
26      * @param x 第一个整数
27      * @param y 第二个整数
28      * @param z 第三个整数
29      */
30     private static void sort(int x, int y, int z) {
31         
32         int[] numArray = {x, y, z};
33         for (int i = 0; i < numArray.length - 1; i++) {
34             for (int j = i + 1; j < numArray.length; j++) {
35                 if (numArray[j] < numArray[i]) {
36                     int temp = numArray[i];
37                     numArray[i] = numArray[j];
38                     numArray[j] = temp;
39                 }
40             }
41         }
42         
43         for (int i = 0; i < numArray.length; i++) {
44             System.out.print(numArray[i] + "  ");
45         }
46         
47         
48     }
49 
50 }

结果如下:

请输入第一个整数
43
请输入第二个整数
61
请输入第三个整数
23
23 43 61

猜你喜欢

转载自www.cnblogs.com/mirai3usi9/p/12794909.html