将两个班级的学生成绩(两个int数组)合起来排序。
将两个班级的学生成绩(两个int数组)合起来排序。
package Demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Score {
public static void main(String[] args) {
int[]classOne= {81,85,83,76,78,90,82,92,79,86};
int[]classTwo= {70,74,72,82,85,78,93,76,79,90,93,94};
int[]grade=new int[classOne.length+classTwo.length];//存放两个班级成绩的数组
for(int i=0;i<classOne.length;i++) {
grade[i]=classOne[i];
}//把classOne的数据添加到新数组grade中
for(int i=classOne.length, j=0;i<classOne.length+classTwo.length;i++,j++) {
grade[i]=classTwo[j];
}//把classTwo的数据添加到新数组grade中
ArrayList arr=new ArrayList();
for(int i=0;i<grade.length;i++) {
arr.add(grade[i]);
}
Collections.sort(arr);//该数组可以通过 Collections.sort进行排序
System.out.println("两个班成绩排序后:"+arr);
double sum=0;
double aveg=0;
for(int i=0;i<grade.length;i++) {
sum+=grade[i];
}
aveg=sum/grade.length;
System.out.println("最高成绩:"+grade[grade.length-1]);
System.out.println("平均成绩:"+aveg);
}
}
有帮助的话采纳一下哦!
合并数组,用冒泡或者其它排序算法排序即可。
冒泡排序
public class Score {
public static void main(String[] args) {
int[] class01 = {2, 3, 4, 10};
int[] class02 = {1, 5, 6, 8};
int[] result = new int[class01.length + class02.length];
System.arraycopy(class01, 0, result, 0, class01.length);
System.arraycopy(class02, 0, result, class01.length, class01.length);
int temp;
for (int i = 0; i < result.length - 1; i++) {
for (int j = 0; j < result.length - 1 - i; j++) {
if (result[j] > result[j + 1]) {
temp = result[j];
result[j] = result[j + 1];
result[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(result));
}
}