import java.util.Arrays; public class Test4 { //冒泡排序 public static void main(String[] args) { int[] a={9,5,6,7,8,4}; int num=0; for (int i = 0; i a.length-1 ; i++) { for (int j = 0; j a.length-1-i; j++) { if (a[j]a[j+1]){ num =a[j+1
public class Test4 { //冒泡排序 public static void main(String[] args) {
int[] a={9,5,6,7,8,4};
int num=0;
for (int i = 0; i <a.length-1 ; i++) {
for (int j = 0; j <a.length-1-i; j++) {
if (a[j]>a[j+1]){
num =a[j+1];
a[j+1]=a[j];
a[j]=num;
}
}
}
System.out.println(Arrays.toString(a));
}
}
Arrays.sort(数组名);
import java.util.Arrays;import java.util.Scanner;
public class Test { //升序排序
public static void main(String[] args) {
System.out.println("请输入五个数");
Scanner scanner = new Scanner(System.in);
int num =scanner.nextInt();
int[] arrs = new int[num];
for (int i = 0; i <num; i++) {
System.out.println("请输入第"+(i+1)+"个数");
int arr =scanner.nextInt();
arrs[i]=arr;
}
Arrays.sort(arrs);
System.out.println(Arrays.toString(arrs));
}
}
求平均值
public class Test2 {public static void main(String[] args) {
double[] arr={10000,20000,30000,40000,50000};
double total= 0;//求平均值
for (int i = 0; i <arr.length; i++) {
total+=arr[i];
}
System.out.println("他的和是"+total);
double avg= total/arr.length;
System.out.println("他的平均值是"+avg);
}
}
新插入一个员工年龄并排序
import java.util.Arrays;import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
System.out.println("请输入原有员工的个数:");
Scanner scanner = new Scanner(System.in);
int age=scanner.nextInt();
int[] arr=new int[age];
for (int i = 0; i <age ; i++) {
System.out.println("请输入第"+(i+1)+"个名字");
int arrs = scanner.nextInt();
arr[i]=arrs;
}
System.out.println("员工的年龄:");
System.out.println(Arrays.toString(arr));
int[] newAge=Arrays.copyOf(arr,arr.length+1);
System.out.println(Arrays.toString(newAge));
System.out.println("请输入新员工的年龄");
int newAges =scanner.nextInt();
newAge[newAge.length-1]=newAges;
Arrays.sort(newAge);
System.out.println(Arrays.toString(newAge));
}