The answer from bleh10 explains the logic, so I won't repeat it here. Nonetheless there are some small details that are either missing or may be misleading which would cause your program not to work as expected, thus leaving you scratching your head as to why.
Firstly, bleh10's answer uses int
arrays, when it is clearly stated in your question that grades are double
.
Secondly, there are some quirks to be aware of when using Scanner
. Refer to this question: Scanner is skipping nextLine() after using next() or nextFoo()?
Finally, I tried to make the below code more "user friendly" by printing appropriate prompts when asking the user to enter values. Also, I changed your print()
method so that it displays a table of the results. I presume you may not yet have learned about method printf()
, so for an explanation of the formatting strings I used, refer to javadoc for class java.util.Formatter
Here is my complete solution - which started with your original code that I modified. Note that I changed the name of your class according to the Java naming conventions.
import java.util.Scanner;
public class Students {
private static final int NUMBER_OF_EXAMS = 3;
private Scanner keyboard;
private double[][] mat;
private double[] examAverages = new double[NUMBER_OF_EXAMS];
private double[] studentAverages;
public void classroom() {
keyboard = new Scanner(System.in);
System.out.print("How many students does your classroom have? ");
// We know that 3 exams were done, but not how many students are in the classroom.
int rows = keyboard.nextInt();
studentAverages = new double[rows];
mat = new double[rows][NUMBER_OF_EXAMS];
double[] examTotals = new double[NUMBER_OF_EXAMS]; // each array element implicitly initialized to zero
double studentTotal = 0.0d;
for (int i = 0; i < rows; i++) {
System.out.println("Enter grades for student " + (i + 1));
studentTotal = 0.0d;
for (int c = 0; c < mat[i].length; c++) {
mat[i][c] = getGrade(c);
studentTotal += mat[i][c];
examTotals[c] += mat[i][c];
}
studentAverages[i] = studentTotal / NUMBER_OF_EXAMS;
}
for (int i = 0; i < NUMBER_OF_EXAMS; i++) {
examAverages[i] = examTotals[i] / rows;
}
}
/**
* Accepts, from user, a single grade for a single exam for a single student.
*
* @param index - exam index
*
* @return Grade entered by user.
*/
private double getGrade(int index) {
if (keyboard.hasNextLine()) {
keyboard.nextLine();
}
System.out.printf("Enter grade %d: ", (index + 1));
double grade = keyboard.nextDouble();
return grade;
}
/**
* Displays table of exam results for all students including each student's average grade
* plus the average result for each exam.
*/
public void print () {
// Print column headers for results table.
System.out.println(" Exam 1 Exam 2 Exam 3 Average");
for (int i = 0; i < mat.length; i++) {
System.out.printf("Student %2d: ", (i + 1));
for(int c = 0; c < mat[i].length; c++) {
System.out.printf("%6.2f ", mat[i][c]);
}
System.out.printf("%.2f%n", studentAverages[i]);
}
System.out.print("Averages: ");
for (int i = 0; i < NUMBER_OF_EXAMS; i++) {
System.out.printf("%6.2f ", examAverages[i]);
}
System.out.println();
}
public static void main(String[] args) {
Students e = new Students();
e.classroom();
e.print();
}
}
Here is the output for a sample run of the above code:
How many students does your classroom have? 2
Enter grades for student 1
Enter grade 1: 70
Enter grade 2: 70
Enter grade 3: 70
Enter grades for student 2
Enter grade 1: 30
Enter grade 2: 30
Enter grade 3: 30
Exam 1 Exam 2 Exam 3 Average
Student 1: 70.00 70.00 70.00 70.00
Student 2: 30.00 30.00 30.00 30.00
Averages: 50.00 50.00 50.00