I am supposed to create a program that evaluates Letter Grades for specified scores such that the user does not enter the number of grades in advance. Instead, the user enters a sentinel value in order to stop entering grades. There should be a maximum limit of 100 grades, after which no more grades can be entered. I displayed a warning about the maximum. The problem is that the programmer does not know in advance how many iterations are necessary for the user-input loop. I am allowed to use whichever loop type I prefer. I ave everything done in the problem except I'm not entirely sure how to get the loop to terminate when a negative value is entered because it needs to terminate AND still display the result of all prior user input. I think I need to use something such as:
while(i !< 0);
Here is my code at the present: Any help would be greatly appreciated!
CODE
import javax.swing.JOptionPane;
public class AssignGrade {
public static void main(String[] args) {
int[] scores = new int[100]; // Array scores
int best = 0; // The best score
char grade; // The grade
// Read scores and find the best score
for (int i = 0; i < 100; i++) {
String scoreString = JOptionPane.showInputDialog("Please note that the maximum number of students is 100. \n Please enter a score (negative to quit):");
// Convert string into integer
scores[i] = Integer.parseInt(scoreString);
if (scores[i] > best)
best = scores[i];}
// Declare and initialize output string
String output = "";
// Assign and display grades
for (int i = 0; i < 100; i++) {
if (scores[i] >= best - 10)
grade = 'A';
else if (scores[i] >= best - 20)
grade = 'B';
else if (scores[i] >= best - 30)
grade = 'C';
else if (scores[i] >= best - 40)
grade = 'D';
else
grade = 'F';
output += "Student " + i + " score is " +
scores[i] + " and grade is " + grade + "\n";
}
// Display the result
JOptionPane.showMessageDialog(null, output);
}
}