# 如何对数组进行排序和显示？ [重复]

##### 问题描述投票：-1回答：1

import java.util.Scanner;

``````//#OfScores is the length of the scores array
public static int numberOfScores = 0;
public static double[] scores = new double[numberOfScores];
public static double sum = 0.0;
public static double place = 0.0;
//Of sorted list
public static double[] sortedList = new double[numberOfScores];

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of scores you'd like to average: ");
numberOfScores = input.nextInt();
scores = new double[numberOfScores];

for (int count = 0; count < scores.length; count++) {
System.out.print("Enter score #" + count + ": ");
place = input.nextDouble();
scores[count] = place;
System.out.println("The score of " + scores[count] + " is " + getGrade(count));
}

sumOfScores();

System.out.println("The average of these scores is: " + averageOfScores());

sort();

System.out.println("The sorted list of scores (above average scores are "
+ "marked with an\n" + "asterisk '*'): " + sortedList[numberOfScores]);
}

public static char getGrade(int i) {
//this
if (scores[i] >= 90.0) {
return 'A';
}
if (scores[i] >= 80.0) {
return 'B';
}
if (scores[i] >= 70.0) {
return 'C';
}
if (scores[i] >= 60.0) {
return 'D';
}
else {
return 'F';
}
}

public static double sumOfScores() {
//sum = 0.0;
for (int i = 0; i < numberOfScores; i++) {
sum += scores[i];
}
return sum;
}

public static double averageOfScores() {
double average = sum / numberOfScores;
return average;
}

public static void sort() {
for (int i = 0; i < scores.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMin = scores[i];
int currentMinIndex;
currentMinIndex = i;

for (int j = i + 1; j < scores.length; j++) {
if (currentMin > scores[j]) {
currentMin = scores[j];
currentMinIndex = j;
}
}

// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
sortedList[currentMinIndex] = scores[i];
scores[i] = currentMin;
}
}
}
``````

}

[运行程序时，我可以输入要平均的分数数量，输入这些分数，获得总和和平均数，但是在对它进行排序以使星号高于平均数时遇到了问题：

90.0的分数是A

[80.0分是B

[70.0分是C

``````at project4.ScoreAnalyzer.sort(ScoreAnalyzer.java:86)

at project4.ScoreAnalyzer.main(ScoreAnalyzer.java:32)
``````
java
##### 1个回答
1

``````for (int count = 0; count < scores.length ; count++)
``````