我正在尝试让我的代码显示最高分,最低分和教师班的平均分。
我希望它显示如下:
得分最高:94.7,杰克
最低分:80.2,Emily
平均得分:85.6
最高等级和平均成绩有效,但最低等级没有。这就是我到目前为止所做的:
import java.util.*;
public class Grades {
public static void main(String[] args) {
ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("How many students are in your class? ");
int totalStudents = Integer.parseInt(input.nextLine());
String[] names = new String[totalStudents];
double[] scores = new double[totalStudents];
double maxGrade = 0;
double minGrade = scores[0];
double avg = 0;
double sum = 0;
for(int i = 0; i < totalStudents; i++){
System.out.print("Name: ");
names[i] = input.next();
System.out.print("Score: ");
scores[i] = input.nextDouble();
sum += scores[i];
if (scores[i] > maxGrade) {
bestStudentPosition.clear();
maxGrade = scores[i];
bestStudentPosition.add(new Integer(i));
}
else if (scores[i] == maxGrade) {
bestStudentPosition.add(new Integer(i));
}
if (scores[i] < minGrade) {
worstStudentPosition.clear();
minGrade = scores[i];
worstStudentPosition.add(new Integer(i));
}
else if (scores[i] == minGrade) {
worstStudentPosition.add(new Integer(i));
}
}
avg = sum/totalStudents;
System.out.print("Highest score: ");
for (Integer position : bestStudentPosition) {
System.out.println(maxGrade + ", " + names[position]);
}
System.out.print("Lowest score: ");
for (Integer position : worstStudentPosition) {
System.out.println(minGrade + ", " + names[position]);
}
System.out.printf("Average: %3.2f", avg);
}
}
任何帮助都将不胜感激。
答案 0 :(得分:2)
scores[i] < minGrade
其中minGrade
最初为0并且您永远不会为其赋予除0以外的任何值。此外,这仅在等级小于0时才有效。
所以,你可能需要做的是,
import java.util.ArrayList;
import java.util.Scanner;
public class Grades {
public static void main (String[] args) {
ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("How many students are in your class? ");
int totalStudents = Integer.parseInt(input.nextLine());
String[] names = new String[totalStudents];
double[] scores = new double[totalStudents];
double maxGrade = 0;
double minGrade = 0;
double avg = 0;
double sum = 0;
for (int i = 0; i < totalStudents; i++) {
System.out.print("Name: ");
names[i] = input.next();
System.out.print("Score: ");
scores[i] = input.nextDouble();
sum += scores[i];
if (i == 0) {
minGrade = scores[0];
}
if (scores[i] > maxGrade) {
bestStudentPosition.clear();
maxGrade = scores[i];
bestStudentPosition.add(new Integer(i));
} else if (scores[i] == maxGrade) {
bestStudentPosition.add(new Integer(i));
}
if (i > 0 && scores[i] < minGrade) {
worstStudentPosition.clear();
minGrade = scores[i];
worstStudentPosition.add(new Integer(i));
} else if (scores[i] == minGrade) {
worstStudentPosition.add(new Integer(i));
}
}
avg = sum / totalStudents;
System.out.print("Highest score: ");
for (Integer position : bestStudentPosition) {
System.out.println(maxGrade + ", " + names[position]);
}
System.out.print("Lowest score: ");
for (Integer position : worstStudentPosition) {
System.out.println(minGrade + ", " + names[position]);
}
System.out.printf("Average: %3.2f", avg);
}
}
在这些条件之前为minGrade
分配初始值scores[0]
。 e.g。
minGrade = scores[0];
这是输出:
你班上有多少学生? 3
姓名:哈利
得分:90.2
姓名:Laurent
得分:99.99
姓名:Darren
得分:98.9
得分最高:99.99,Laurent
最低分:90.2,哈利
平均值:96.36