[英]Formatting my Output Correctly
I'm trying to make my code will display the highest score, lowest score, and average score of a teacher's class. 我正在尝试让我的代码显示教师班级的最高分数,最低分数和平均分数。
I want it to display like this: 我希望它显示如下:
Highest Score: 94.7, Jake 最高分:94.7,杰克
Lowest Score: 80.2, Emily 最低分数:80.2,艾米丽
Average Score: 85.6 平均分数:85.6
The highest grade and the average grade works, but the lowest grade doesn't. 最高成绩和平均成绩有效,最低成绩则无效。 This is what I have so far: 这是我到目前为止的内容:
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);
}
}
Any help would be appreciated. 任何帮助,将不胜感激。
scores[i] < minGrade
where minGrade
is 0 initially and you are never assigning it any value other than 0. Also, this will only work if grade will be less than 0. 其中minGrade
最初为0,并且您永远不会为其分配除0之外的任何值。而且,这仅在等级小于0时才有效。
So, probably what you need to do is, 因此,可能您需要做的是,
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);
}
}
assign minGrade
with initial value of scores[0]
before these conditions. 在这些条件之前,将minGrade
分配为scores[0]
初始值。 eg 例如
minGrade = scores[0];
This is the output: 这是输出:
How many students are in your class? 你班上有多少学生? 3 3
Name: Harry 姓名:哈里
Score: 90.2 得分:90.2
Name: Laurent 姓名:洛朗(Laurent)
Score: 99.99 得分:99.99
Name: Darren 姓名:达伦
Score: 98.9 得分:98.9
Highest score: 99.99, Laurent 最高分:99.99,洛朗
Lowest score: 90.2, Harry 最低分:90.2,哈里
Average: 96.36 平均:96.36
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.