
本文档旨在指导开发者如何对Java程序中的数组元素进行排序,并以表格形式输出排序后的结果,同时保留原始索引信息。通过修改现有的`selectionSort`方法,并结合索引数组,实现对用户输入的测试成绩进行排序并输出,保证输出结果的准确性和可读性。
原程序存在的问题在于,selectionSort方法直接对数组元素进行排序,导致元素与原始索引之间的对应关系丢失,无法按照“测试1”、“测试2”的顺序正确输出排序后的成绩。同时,程序在排序时需要注意只对用户实际输入的成绩进行排序,而不是整个数组。
为了解决上述问题,我们需要以下步骤:
以下是修改后的代码示例:
立即学习“Java免费学习笔记(深入)”;
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);
}
}代码解释:
修改后的程序可以正确输出排序后的测试成绩表格,并保留原始的测试编号信息。
通过创建索引数组并修改排序算法,我们可以实现对Java程序中的数组元素进行排序,并以表格形式输出排序后的结果,同时保留原始索引信息。这种方法可以应用于各种需要对数据进行排序并保留原始顺序的场景。
以上就是Java 数组排序与索引输出:实现测试成绩排序表格的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号