I have completed the first two steps of my assignment as to how my teacher wanted me to, which were to: calculate and print the average score for each exam calculate and print the average score for each student, and the top scoring student
and now I can't figure out how to get it so it displays the top THREE scoring student's and their scores
I want to do this without sorting the array is ascending/descending order
My code so far is:
public class GradeBook {
public static void main(String[] args) {
String[] names = {"Alex", "Barry","Cindy", "Deb", "Eric", "Fran", "Gary", "Helen"};
int[][] grades = {
{ 77, 83, 96 },
{ 88, 67, 78 },
{ 92, 77, 76 },
{ 94, 42, 81 },
{ 99, 54, 72 },
{ 90, 46, 54 },
{ 76, 59, 88 },
{ 94, 69, 88 }
};
int rSize = grades[0].length;
int cSize = grades.length;
double avg = 0;
double maxNum = 0;
double avgSc = 0;
ArrayList<Double> avgScores = new ArrayList();
ArrayList topThree = new ArrayList();
// calculate and print the average score for each exam
for (int r = 0; r < rSize; r++) {
avg = 0;
for (int c = 0; c < cSize; c++) {
avg += grades[c][r];
}
avg /= cSize;
DecimalFormat a = new DecimalFormat("#.##");
double x = Double.parseDouble(a.format(avg));
System.out.println("\nThe average grade for Exam #" + (r+1) + ": " +x);
}
// calculate and print the average score for each student, and the top scoring student
for (int c = 0; c < cSize; c++) {
avgSc = 0;
for (int r = 0; r < rSize; r++) {
avgSc += grades[c][r];
}
avgSc /= rSize;
DecimalFormat a = new DecimalFormat("#.##");
double x = Double.parseDouble(a.format(avgSc));
System.out.println("\nThe average grade for " + names[c] + ": " +x);
avgScores.add(avgSc);
}
maxNum = 0;
int topI = -1;
for (int i = 0; i<cSize; i++) {
if (avgScores.get(i) > maxNum){
maxNum = avgScores.get(i);
topI = i;
}
else {
continue;
}
}
DecimalFormat a = new DecimalFormat("#.##");
double x = Double.parseDouble(a.format(maxNum));
System.out.println("\nThe top scoring student is: " + names[topI] + ", who has an average score of: " + x);
double top1 = 0;
double top2 = 0;
double top3 = 0;
String name1 = "";
String name2 = "";
String name3 = "";
// Some loop that gets the top 3 students and their scores
}
}
Aucun commentaire:
Enregistrer un commentaire