Java程序:利用selectionSort实现带原始序号的成绩升序表格输出问题
问题说明
现有Java程序可收集最多25个测试成绩,存入数组后计算平均值,并以表格形式输出未排序的成绩及平均值。程序中存在未使用的selectionSort排序方法,要求:
- 使用该方法实现按成绩升序显示带原输入序号的成绩表格
- 不能修改
main方法中的任何代码 - 提示需创建存储原数组索引的新数组
之前直接调用排序方法会打乱原成绩数组,导致输出的序号和成绩不匹配,无法达到预期效果。
解决方案
核心思路是不对原成绩数组直接排序,而是对索引数组排序:通过比较对应位置的成绩大小,调整索引数组的顺序,这样就能保留每个成绩的原输入序号,再根据排序后的索引数组输出对应成绩即可。
修改后的完整代码
import java.util.Scanner; public class ArrayIntro2 { public static void main(String[] args) { //integer array int [] TestGrades = new int[25]; //creating object of ArrayIntro2T ArrayIntro2T pass = new ArrayIntro2T(TestGrades, 0, 0, 0); //getting total and filling array int scoreCount = ArrayIntro2T.FillArray(TestGrades, 0); //get average score double avg = pass.ComputeAverage(TestGrades, scoreCount); //outputting table ArrayIntro2T.OutputArray(TestGrades,scoreCount,avg); } } //new class to store methods class ArrayIntro2T{ //variable declaration double CalcAvg = 0; int ScoreTotal = 0; int ScoreCount = 0; int [] TestGrades = new int[25]; //constructor public ArrayIntro2T(int [] TestGradesT, int ScoreCountT, double CalcAvgT, int ScoreTotalT) { TestGrades = TestGradesT; ScoreCount = ScoreCountT; CalcAvg = CalcAvgT; ScoreTotal = ScoreTotalT; } //method to fill array public static int FillArray(int [] TestGrades, int ScoreCount) { Scanner scan = new Scanner(System.in); System.out.println("Please enter test scores one at a time, up to 25 values or enter -1 to quit" ); TestGrades[ScoreCount]= scan.nextInt(); if(TestGrades[ScoreCount]==-1) { System.out.println("You have chosen to quit "); } while(TestGrades[ScoreCount]>=0 && ScoreCount<24) // 修正原循环边界,避免数组越界 { ScoreCount++; System.out.println("Enter the next test score or -1 to finish "); TestGrades[ScoreCount] = scan.nextInt(); } return ScoreCount; } //method to compute average public double ComputeAverage(int [] TestGrades,int ScoreCount) { for(int i=0; i<ScoreCount;i++) { ScoreTotal += TestGrades[i]; CalcAvg = (double)ScoreTotal/(double)ScoreCount; } return CalcAvg; } // 修改排序逻辑:对索引数组排序,依据对应成绩的大小 public static int[] selectionSortIndices(int[] TestGrades, int scoreCount){ int[] indices = new int[scoreCount]; // 初始化索引数组,对应原输入序号的索引 for(int i=0; i<scoreCount; i++){ indices[i] = i; } int startScan, index, minIndex; for(startScan=0; startScan<(scoreCount-1);startScan++){ minIndex = startScan; for(index = startScan+1;index<scoreCount; index++){ // 比较对应索引的成绩大小 if(TestGrades[indices[index]] < TestGrades[indices[minIndex]]) { minIndex=index; } } // 交换索引位置 int temp = indices[minIndex]; indices[minIndex] = indices[startScan]; indices[startScan] = temp; } return indices; } //method to output scores and average public static void OutputArray(int [] TestGrades,int ScoreCount, double CalcAvg) { // 输出未排序表格 System.out.println("=== 未排序测试成绩表格 ==="); System.out.println("Grade Number\t\tGrade Value"); for(int i=0; i<ScoreCount;i++) { System.out.println((i+1)+"\t\t\t"+TestGrades[i]); } System.out.printf("Calculated Average\t%.2f%%\n\n", CalcAvg); // 生成排序后的索引数组 int[] sortedIndices = selectionSortIndices(TestGrades, ScoreCount); // 输出已排序表格 System.out.println("=== 已排序测试成绩表格 ==="); System.out.println("Grade Number\t\tGrade Value"); for(int i=0; i<ScoreCount;i++){ int originalIndex = sortedIndices[i]; System.out.println((originalIndex+1)+"\t\t\t"+TestGrades[originalIndex]); } } }
关键修改点说明
- 新增
selectionSortIndices方法:不再直接排序成绩数组,而是创建一个索引数组,根据对应位置的成绩大小对索引进行排序,这样保留了每个成绩的原输入序号。 - 扩展
OutputArray方法:在原输出逻辑后,调用新的排序方法获取有序索引,再遍历索引数组输出原序号和对应成绩,实现带原序号的升序表格输出。 - 修正原循环边界:原
FillArray方法中的循环条件ScoreCount<=25会导致数组越界,改为ScoreCount<24(因为数组长度是25,索引最大为24)。
这样修改后,无需改动main方法,调用OutputArray时会自动输出未排序和已排序两个表格,完全符合需求。
内容的提问来源于stack exchange,提问作者carbanak
相关产品推荐
相关产品推荐

