首页 > 解决方案 > 如何对数组进行排序并显示?

问题描述

这是我的代码: package project4;

导入 java.util.Scanner;

公共类 ScoreAnalyzer {

//#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;
        }
    }
}

}

该程序的目的是先提示用户输入分数的数量,然后允许用户输入分数的数量。在输入每个分数时,程序应该为输入的每个分数给他们一个等级。然后程序应该使用一个方法来找到总和,将该总和传递给一个求平均值的方法,并在一个方法中对数组进行排序。排序后的数组应该与平均值一起显示,每个高于平均值的分数应该旁边带有星号。

当我运行程序时,我可以输入我想要平均的分数,输入这些分数,获得总和和平均值,但是我在排序它以使星号高于平均数时遇到问题:

输入您想要平均的分数:3

输入分数 #0:90

90.0的分数是A

输入分数 #1:80

80.0的分数是B

输入分数 #2:70

70.0的分数是C

这些分数的平均值是:80.0

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:2

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

at project4.ScoreAnalyzer.main(ScoreAnalyzer.java:32)

标签: java

解决方案


for循环的终止条件有问题。由于数组索引以 开头,因此对于大小为 的数组,0最高索引将上升到。但是,您正在使用的访问元素的, 将在您的循环中上升,这就是. 改变循环如下:23countscores[]3forArrayOutOfBoundsExceptionfor

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

推荐阅读