[英]Sorting array elements and indices, and outputting in table form
下面的 Java 程序要求用户提供最多 25 个测试分数,然后将它们存储在一个数组中,对它们进行平均,并以表格形式打印出输入的测试成绩以及计算出的平均值。 有一个未使用的排序算法作为它自己的方法出现,名为 selectionSort。 这实际上是来自教科书。
我需要使用该方法对数组进行排序并创建一个额外的 output 就像下面显示的第二个示例,其中测试分数按升序显示。 我唯一的提示是我需要制作另一个数组的第一个数组的索引。
我不应该在 main 方法中放置任何额外的代码,所以我假设我需要一个单独的方法? 或者我可以将所有附加代码放在 selectionSort 方法中,这样我只需要调用一个方法吗? 我所了解的是 selectionSort 方法对元素进行排序,而不是对索引进行排序,因此它不会像预期的那样显示测试 1、2、3。 所以我也需要对索引进行排序,然后以某种方式打印两者? 我该怎么做呢? 谢谢
现在的output是这样的。
我需要这样一个额外的 output。 “分类考试成绩表”
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);
}
}
//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[] TestGrades){
int startScan, index, minIndex, minValue;
for(startScan=0; startScan<(TestGrades.length-1);startScan++){
minIndex = startScan;
minValue = TestGrades[startScan];
for(index = startScan+1;index<TestGrades.length; index++){
if(TestGrades[index]<minValue)
{
minValue=TestGrades[index];
minIndex=index;
}
}
TestGrades[minIndex]=TestGrades[startScan];
TestGrades[startScan]=minValue;
}
}
//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%%", CalcAvg);
}
}
我试过在 main 方法中调用 selectionSort 方法,也尝试使用 Array.sort 尽管它们产生相同的结果。 当我这样做时,我得到一个 output,如下所示:
你为什么要考虑对索引进行排序? 您期望的已排序 output 不会更改索引的顺序。 在main
方法中添加对selectionSort()
的调用是有意义的,但如果您不应该这样做,则可以将其添加到FillArray()
OutputArray()
开头。
至于 output 的问题,您需要考虑仅对用户输入的分数进行排序,而不是对数组中的每个条目进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.