利用数组存储java课学生成绩,并统计最高分、最低分、按成绩高低排序(冒泡法?

如题所述

import java.util.Arrays;
public class ScoreAnalyzer {
public static void main(String[] args) {
int[] scores = {88, 92, 75, 99, 64, 72, 85, 91, 78, 80};
int maxScore = getMaxScore(scores);
int minScore = getMinScore(scores);
int[] sortedScores = bubbleSort(scores);
System.out.println("最高分:" + maxScore);
System.out.println("最低分:" + minScore);
System.out.println("按成绩高低排序:" + Arrays.toString(sortedScores));
}
public static int getMaxScore(int[] scores) {
int max = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] > max) {
max = scores[i];
}
}
return max;
}
public static int getMinScore(int[] scores) {
int min = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] < min) {
min = scores[i];
}
}
return min;
}
public static int[] bubbleSort(int[] scores) {
int[] sortedScores = Arrays.copyOf(scores, scores.length);
for (int i = 0; i < sortedScores.length - 1; i++) {
for (int j = 0; j < sortedScores.length - i - 1; j++) {
if (sortedScores[j] < sortedScores[j + 1]) {
int temp = sortedScores[j];
sortedScores[j] = sortedScores[j + 1];
sortedScores[j + 1] = temp;
}
}
}
return sortedScores;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-03-13
public static void sortScore(int[] scores)
{

int temp;

for(int j= 0; j< scores.length; j++)
//外层循环控制排序趟数

{

for(int i=0; i< scores.length-1-j; i++ )
//内层循环控制每一趟排序多少次

{

if(scores[i] > scores[i+1])

{

temp = scores[i];
scores[i]= scores[i+1];
scores[i+1] = temp;
}
}
}

int min = scores[0];
int max = scores[scores.length-1];
System.out.println("最低分:" + min);

System.out.println("最高分:" + max);

//输出排序结果

System.out.println("按从小到大排序后的成绩:");

for(int i = 0; i< scores.length; i++)
{

System.out.print(scores[i] + "\t");

}
}