2020年月20日Java学习日记
学习内容
整数排序
编写函数重载,分别将两个整数升序排列后输出、三个整数升序排列后输出、四个整数升序排列后输出
import java.util.Scanner; public class Study { public static void sort(int a[], int n) { int temp; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } //两个整数排序 public static void sort(int a, int b) { int arr[] = { a, b }; sort(arr, 2); } //三个整数排序 public static void sort(int a, int b, int c) { int arr[] = { a, b, c }; sort(arr, 3); } //四个整数排序 public static void sort(int a, int b, int c, int d) { int arr[] = { a, b, c, d }; sort(arr, 4); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int a1, a2; int b1, b2, b3; int c1, c2, c3, c4; System.out.print("请输入两个整数:"); a1 = in.nextInt(); a2 = in.nextInt(); System.out.print("排序后:"); sort(a1, a2); System.out.print("请输入三个整数:"); b1 = in.nextInt(); b2 = in.nextInt(); b3 = in.nextInt(); System.out.print("排序后:"); sort(b1, b2, b3); System.out.print("请输入四个整数:"); c1 = in.nextInt(); c2 = in.nextInt(); c3 = in.nextInt(); c4 = in.nextInt(); System.out.print("排序后:"); sort(c1, c2, c3, c4); } }