I created a String[][] grades, who's size is set by number of students User enters.
The array is then populated with my first for loop, and the next for loop is supposed to print out each students date line by line.
If I only enter 1 Student and fill in the data, it prints out perfectly.
But if I enter 2 or more students, things go wrong:
- It prints the information twice side by side, and
- It only prints the last entered data, rather than e.g. Jane & Jo's data
Could someone take a look at my two for loops and see if they can spot what I am doing wrong please.
import javax.swing.JOptionPane;
import javax.swing.*;
public class StudentGrades {
static double percentage;
static char grade;
public static void main (String[] args) {
String name = "", temp;
int students, count = 0, quiz1 = 0, quiz2 = 0, midterm_exam = 0, final_exam = 0;
temp = JOptionPane.showInputDialog(null, "How many students in class?");
students = Integer.parseInt(temp);
String[][] grades = new String[students][5];
while (students > count) {
name = JOptionPane.showInputDialog(null, "Enter Student Name");
temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Quiz 1 score out of 10");
quiz1 = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Quiz 2 score out of 10");
quiz2 = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Midterm Exam score out of 100");
midterm_exam = Integer.parseInt(temp);
temp = JOptionPane.showInputDialog(null, "Enter " + name + "\'s Final Exam score out of 100");
final_exam = Integer.parseInt(temp);
count++;
for (int a = 0; a < grades.length; a++) {
grades[a][0] = name;
grades[a][1] = String.valueOf(quiz1);
grades[a][2] = String.valueOf(quiz2);
grades[a][3] = String.valueOf(midterm_exam);
grades[a][4] = String.valueOf(final_exam);
}
}
System.out.println("Name\tQuiz 1\tQuiz2\tMidterm\tFinal\tPercentage\tGrade");
for (int a = 0; a < grades.length; a++) {
for (int b = 0; b < grades.length; b++) {
grades[a][0] = name;
grades[a][1] = String.valueOf(quiz1);
grades[a][2] = String.valueOf(quiz2);
grades[a][3] = String.valueOf(midterm_exam);
grades[a][4] = String.valueOf(final_exam);
System.out.print(
grades[a][0] + "\t" +
grades[a][1] + "\t" +
grades[a][2] + "\t" +
grades[a][3] + "\t" +
grades[a][4] + "\t" +
percentage(quiz1, quiz2, midterm_exam, final_exam) + "%\t\t" +
grade(percentage)
);
}
System.out.println("\n");
}
}
public static double percentage(int i, int j, int k, int l) {
percentage = (
(i * 1.25) +
(j * 1.25) +
(k * 0.25) +
(l * 0.50)
);
return percentage;
}
public static char grade(double p) {
p = percentage;
if (p >= 90) {
grade = 'A';
}
else if (p >= 80 && grade < 90) {
grade = 'B';
}
else if (p >= 70 && grade < 80) {
grade = 'C';
}
else if (p >= 60 && grade < 70) {
grade = 'D';
}
else {
grade = 'F';
}
return grade;
}
}