本文档旨在指导 Java 初学者如何对用户输入的测试分数进行排序,并以表格形式输出,同时保留原始输入的索引信息。我们将利用选择排序算法对分数进行排序,并调整输出方式,使其能够清晰地展示排序后的分数及其对应的原始输入序号,最终实现一个完整的排序输出表格。
原有的代码存在一个问题,即 selectionSort 方法直接对 TestGrades 数组进行排序,导致输出时 grade number 和 grade value 对应错误。 期望的输出是:先按照用户输入的顺序输出 grade number 和 grade value,然后再输出一个排序后的 grade number 和 grade value,且 grade number 仍然是按照用户输入的顺序来的。
核心思路是:
以下是修改后的代码:
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);
// Sort and output sorted table
ArrayIntro2T.sortAndOutputArray(TestGrades, scoreCount);
}
}
// 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 < 25) {
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[] arr, int[] indices, int length) {
for (int i = 0; i < length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < length; j++) {
if (arr[indices[j]] < arr[indices[minIndex]]) {
minIndex = j;
}
}
// Swap indices, not the values in arr
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("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 sortAndOutputArray(int[] TestGrades, int scoreCount) {
// 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("\nTable of sorted test scores");
System.out.println("Grade Number\t\tGrade Value");
for (int i = 0; i < scoreCount; i++) {
System.out.println((indices[i] + 1) + "\t" + "\t" + "\t" + TestGrades[indices[i]]);
}
}
}sortAndOutputArray 方法:
selectionSort 方法:
修改后的代码可以正确地对用户输入的测试分数进行排序,并以表格形式输出,同时保留原始输入的索引信息。 输出结果包含两个表格,第一个表格是按照用户输入的顺序输出的,第二个表格是按照分数大小排序后输出的。
通过创建索引数组,并在排序过程中交换索引,可以实现在不改变原始数组顺序的情况下,对数组进行排序,并保留原始索引信息。 这种方法在需要同时访问排序后的数据和原始数据时非常有用。