package 面试题;
import java.util.ArrayList;
import java.util.Scanner;
public class Test4 {
/*
* 6、键盘录入5个数据,存储到一个数组中,取最大值和最小值。
*/
public static void main(String[] args) {
Integer[] arr = getArray();
sort(arr);
System.out.println("min:" + arr[0]);
System.out.println("max:" + arr[arr.length - 1]);
}
public static void sort(Integer[] arr) {
//冒泡
/*for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}*/
//快速
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr>arr[j]) {
swap(arr, i, j);
}
}
}
}
private static void swap(Integer[] arr, int i, int j) {
Integer temp = arr;
arr = arr[j];
arr[j] = temp;
}
public static Integer[] getArray() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入5个数据:");
ArrayList<Integer> list = new ArrayList<>();
while (true) {
String s = sc.nextLine();
list.add(new Integer(s));
if (list.size() == 5) {
break;
}
}
return list.toArray(new Integer[list.size()]); //这一句话我看不懂
}
}
|