15 输入三个整数x,y,z,请把这三个数由小到大输出。
2023-10-05 本文已影响0人
北极的大企鹅
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1 public class _015ThreeNumberSort {
2
3 public static void main(String[] args) {
4 while (true) {
5 threeNumberSort();
6 }
7 }
8
9 private static void threeNumberSort() {
10 int x, y, z;
11 Scanner in = new Scanner(System.in);
12 System.out.println("请输入第一个数:");
13 x = in.nextInt();
14 System.out.println("请输入第二个数:");
15 y = in.nextInt();
16 System.out.println("请输入第三个数:");
17 z = in.nextInt();
18
19 numberSort(x, y, z);
20 }
21
22 private static void numberSort(int x, int y, int z) {
23 int temp = 0;
24 if (x > y) {
25 temp = x;
26 x = y;
27 y = temp;
28 }
29 if (x > z) {
30 temp = x;
31 x = z;
32 z = temp;
33 }
34 if (y > z) {
35 temp = y;
36 y = z;
37 z = temp;
38 }
39
40 System.out.println("三个数由小到大排列为:" + x + " " + y + " " + z);
41 }
42
43 }