Reputation: 23
I'm working on a java program that takes grades inputted gives the total number inputted and the average however I'm having trouble figuring out how to do the highest grade entered currently I"m using a ton of nested else if statements, but there has to be an easy way to do this rather than typing 100 else if statements heres my code. I put a comment where the else if statements begin that determine the highest grade
//Jonathan Towell
//This program will take grade inputs and then display the amount of A's B's C's D's and F's
import javax.swing.JOptionPane;
public class Grades
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Welcome to Jonathan's Gradebook");
int A = 0, B = 0, C = 0, D = 0, F = 0;
int totalGrades = 0, grade = 0, averageGrade = 0, highestGrade = 0;
while (grade >= 0)
{
grade = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade followed by the enter key\n when finished enter -1"));
if (grade <= 100 && grade >= 90)
{
A = A + 1;
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
if (grade == 100) // Determines highest grade
{
highestGrade = 100;
}
else if (grade == 99)
{
highestGrade = 99;
}
else if (grade == 98)
{
highestGrade = 98;
}
else if (grade == 97)
{
highestGrade = 97;
}
else if (grade == 96)
{
highestGrade = 96;
}
else if (grade == 95)
{
highestGrade = 95;
}
else if (grade == 94);
{
highestGrade = 94;
}
}
else if (grade > 100)
{
JOptionPane.showMessageDialog(null, "No extra credit for this assignment\n please enter a value less than 100");
}
else if (grade <= 89 && grade >= 80)
{
B = B + 1;
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
}
else if (grade <= 79 && grade >= 70)
{
C = C + 1;
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
}
else if (grade <= 69 && grade >= 60)
{
D = D + 1;
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
}
else if (grade <= 59 && grade >= 0)
{
F = F + 1;
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
}
}
averageGrade = (averageGrade / totalGrades);
JOptionPane.showMessageDialog(null, "Total number of grades entered was " + totalGrades + "\n The average grade was: " + averageGrade + " percent." + "\nA's: " + A + "\nB's: " + B + "\nC's: " + C + "\nD's: " + D + "\nF's: " + F + "\nThe highest grade earned was: " + highestGrade + " percent.");
System.exit(0);
}
}
Upvotes: 0
Views: 160
Reputation: 552
Jonathon,
You repeat a lot of your code. IN each of the if clauses you repeat the same two lines of code. Why not do these two lines of code outside of the if clause at the end. These two lines are repeated: totalGrades = totalGrades + 1; averageGrade = averageGrade + grade;
With regards to the huge if statement for if (grade == 100) // Determines highest grade {
Replace it with: grade = (grade > highestGrade ? highestGrade : grade);// Determines highest grade
You could also use: highestGrade = Math.max(highestGrade, grade);
I also re-ordered part of the code. Your if statement says "if between 100 and 90" then goes to "if above 100". That's just confusing. I ordered it as "if above 100" and then "if between 100 and 90", much more logical.
The final code should look like this:
public class Grades
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "Welcome to Jonathan's Gradebook");
int A = 0, B = 0, C = 0, D = 0, F = 0;
int totalGrades = 0, grade = 0, averageGrade = 0, highestGrade = 0;
while (grade >= 0)
{
grade = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade followed by the enter key\n when finished enter -1"));
if (grade > 100) {
JOptionPane.showMessageDialog(null, "No extra credit for this assignment\n please enter a value less than 100");
} else if (grade <= 100 && grade >= 90) {
A = A + 1;
grade = (grade > highestGrade ? highestGrade : grade);// Determines highest grade
} else if (grade <= 89 && grade >= 80) {
B = B + 1;
} else if (grade <= 79 && grade >= 70) {
C = C + 1;
} else if (grade <= 69 && grade >= 60) {
D = D + 1;
} else if (grade <= 59 && grade >= 0) {
F = F + 1;
}
totalGrades = totalGrades + 1;
averageGrade = averageGrade + grade;
averageGrade = (averageGrade / totalGrades);
JOptionPane.showMessageDialog(null, "Total number of grades entered was " + totalGrades + "\n The average grade was: " + averageGrade + " percent." + "\nA's: " + A + "\nB's: " + B + "\nC's: " + C + "\nD's: " + D + "\nF's: " + F + "\nThe highest grade earned was: " + highestGrade + " percent.");
System.exit(0);
}
}
Let me know if this seems better for you.
Upvotes: 1
Reputation: 1150
To make this more manageable, you can consider using a Factory Pattern with polymorphism and have all the grade brackets(if, else-if) implemented in their own separate class.
Upvotes: 2