
本文档旨在指导读者如何对 Java 数组中的元素进行排序,并以表格形式输出排序前后的数据。我们将使用选择排序算法对用户输入的测试分数进行排序,并展示如何保留原始索引信息,最终以清晰的表格形式呈现排序结果。本文档将提供详细的代码示例和步骤说明,帮助读者理解并实现数组排序和格式化输出。
我们需要对用户输入的测试分数进行排序,并在排序后以表格形式输出。关键在于,需要保留原始输入顺序的索引信息,以便在排序后的表格中正确显示“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 unsorted table
System.out.println("Original Test Scores:");
ArrayIntro2T.OutputArray(TestGrades, scoreCount, avg);
// Sort and output sorted table
System.out.println("\nTable of sorted test scores");
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();
if (TestGrades[ScoreCount] == -1) {
break;
}
}
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
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 OutputSortedArray(int[] TestGrades, int ScoreCount, double CalcAvg) {
int[] indices = new int[TestGrades.length];
for (int i = 0; i < TestGrades.length; i++) {
indices[i] = i;
}
selectionSort(TestGrades, indices, ScoreCount);
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]]);
}
System.out.printf("Calculated Average\t" + "%.2f%%\n", CalcAvg);
}
}程序首先会要求用户输入测试分数,然后输出原始的测试分数表格,接着输出排序后的测试分数表格。排序后的表格会显示按照分数从小到大排列的成绩,并保留原始的“Grade Number”。
立即学习“Java免费学习笔记(深入)”;
通过创建索引数组并对其进行排序,我们可以在对数组元素进行排序的同时,保留原始的索引信息。这种方法在需要维护原始数据顺序的场景下非常有用。本教程提供了一个完整的示例,展示了如何在 Java 中实现数组排序和格式化输出,希望能够帮助读者更好地理解和应用相关技术。
以上就是Java 数组排序及表格输出教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号