[英]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.