java - 如何对数组进行排序并显示?
问题描述
这是我的代码: 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)
解决方案
for
循环的终止条件有问题。由于数组索引以 开头,因此对于大小为 的数组,0
最高索引将上升到。但是,您正在使用的访问元素的, 将在您的循环中上升,这就是. 改变循环如下:2
3
count
scores[]
3
for
ArrayOutOfBoundsException
for
for (int count = 0; count < scores.length ; count++)
推荐阅读
- java - 为什么我在 JLabels 上的图像图标在运行时像素化,但在设计模式下却没有?
- playback - 在播放框架 1.5.x 中增加最大标题大小
- html - 修复表格单元格中的垂直对齐位置:绝对
- tableau-api - 为什么 Tableau LOD 计算会显示一系列值而不是总和?
- angular - Azure ADO Build + deploy to azure web app 仍然显示“欢迎节点开发者”页面
- ruby - 在 Rails 6 中调用 Ajax 后如何从控制器端重定向?
- perl - 如何在for循环xml中添加/删除值?
- ios - 填充列表以避免键盘重叠
- angular - 使用 HTML 5 播放器在录制时播放文件
- html - 我的引导下拉类无法显示下拉菜单