Java 数组排序与索引输出:实现测试成绩排序表格

本文档旨在指导开发者如何对Java程序中的数组元素进行排序,并以表格形式输出排序后的结果,同时保留原始索引信息。通过修改现有的`selectionSort`方法,并结合索引数组,实现对用户输入的测试成绩进行排序并输出,保证输出结果的准确性和可读性。

问题分析

原程序存在的问题在于,selectionSort方法直接对数组元素进行排序,导致元素与原始索引之间的对应关系丢失,无法按照“测试1”、“测试2”的顺序正确输出排序后的成绩。同时,程序在排序时需要注意只对用户实际输入的成绩进行排序,而不是整个数组。

解决方案

为了解决上述问题,我们需要以下步骤:

  1. 创建索引数组: 创建一个与原始成绩数组大小相同的索引数组,初始化为0到scoreCount-1。
  2. 修改排序算法: 修改selectionSort方法,使其对索引数组进行排序,而不是直接对成绩数组进行排序。在比较成绩时,使用索引数组访问成绩数组。
  3. 输出排序后的表格: 在输出表格时,使用排序后的索引数组来访问成绩数组,从而按照排序后的顺序输出成绩和对应的测试编号。

代码实现

以下是修改后的代码示例:

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);

        // outputting sorted table
        ArrayIntro2T.OutputSortedArray(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 < TestGrades.length - 1) {
            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];
            CalcAv

g = (double) ScoreTotal / (double) ScoreCount; } return CalcAvg; } public static void selectionSort(int[] TestGrades, int[] indices, int scoreCount) { for (int i = 0; i < scoreCount - 1; i++) { int minIndex = i; for (int j = i + 1; j < scoreCount; j++) { if (TestGrades[indices[j]] < TestGrades[indices[minIndex]]) { minIndex = j; } } // Swap indices, not the grades themselves int temp = indices[i]; indices[i] = indices[minIndex]; indices[minIndex] = temp; } } // method to output scores and average public static void OutputArray(int[] TestGrades, int ScoreCount, double CalcAvg) { System.out.println("Table of unsorted test scores"); 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", CalcAvg); } public static void OutputSortedArray(int[] TestGrades, int ScoreCount, double CalcAvg) { // Create an array of indices int[] indices = new int[ScoreCount]; for (int i = 0; i < ScoreCount; i++) { indices[i] = i; } // Sort the indices array based on the values in TestGrades selectionSort(TestGrades, indices, ScoreCount); System.out.println("Table of sorted test scores"); System.out.println("Grade Number\t\tGrade Value"); for (int i = 0; i < ScoreCount; i++) { System.out.println((i + 1) + "\t" + "\t" + "\t" + TestGrades[indices[i]]); } System.out.printf("Calculated Average\t" + "%.2f%%\n", CalcAvg); } }

代码解释:

  1. OutputSortedArray方法:
    • 创建索引数组indices,并初始化为0到scoreCount-1。
    • 调用selectionSort方法,传入成绩数组、索引数组和成绩数量。
    • 遍历排序后的索引数组,使用TestGrades[indices[i]]访问成绩数组,按照排序后的顺序输出成绩和对应的测试编号。
  2. selectionSort方法:
    • 修改排序逻辑,使其对索引数组进行排序。
    • 在比较成绩时,使用TestGrades[indices[j]]和TestGrades[indices[minIndex]]访问成绩数组。
    • 交换索引数组中的元素,而不是直接交换成绩数组中的元素。

运行结果

修改后的程序可以正确输出排序后的测试成绩表格,并保留原始的测试编号信息。

注意事项

  • 确保selectionSort方法中的循环条件和数组访问方式正确,避免数组越界错误。
  • 在输出表格时,使用排序后的索引数组来访问成绩数组,确保输出顺序正确。
  • 可以根据实际需求,修改排序算法,例如使用更高效的排序算法(如快速排序、归并排序)来提高排序效率。

总结

通过创建索引数组并修改排序算法,我们可以实现对Java程序中的数组元素进行排序,并以表格形式输出排序后的结果,同时保留原始索引信息。这种方法可以应用于各种需要对数据进行排序并保留原始顺序的场景。