首页 > Java > java教程 > 正文

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

聖光之護
发布: 2025-10-21 09:54:22
原创
483人浏览过

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

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

问题分析

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

解决方案

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

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

代码实现

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

立即学习Java免费学习笔记(深入)”;

简篇AI排版
简篇AI排版

AI排版工具,上传图文素材,秒出专业效果!

简篇AI排版 554
查看详情 简篇AI排版
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];
            CalcAvg = (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程序中的数组元素进行排序,并以表格形式输出排序后的结果,同时保留原始索引信息。这种方法可以应用于各种需要对数据进行排序并保留原始顺序的场景。

以上就是Java 数组排序与索引输出:实现测试成绩排序表格的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号