For Loop NOT Incrementing...
I know this is a really stupid, mundane thing, but for some reason I can't to get anything within the for loop to increment.
Here is he code:
Code:
import javax.swing.JOptionPane;
public class Grades {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter the number of students:");
int students = Integer.parseInt(input);
int[] numStudents = new int[students];
for(int i = 0; i < numStudents.length; i++) {
String inputGrade = JOptionPane.showInputDialog("Enter the score of student " + (numStudents[i] + 1) + ":");
int grade = Integer.parseInt(inputGrade);
int best = 100;
if(grade >= best - 10) {
System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is A");
} else if(grade >= best - 20) {
System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is B");
} else if(grade >= best - 30) {
System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is C");
} else if(grade >= best - 40) {
System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is D");
} else {
System.out.println("Student " + (numStudents[i] + 1) + " score is " + grade + " and grade is F");
}
}
}
}
Example output:
If I was to enter 3 student and their grades, for all it says, "Student 1 score is..." I have no idea why it won't increment.
Any suggestions? Thanks in advance for the help...
Re: For Loop NOT Incrementing...
Look for this line
String inputGrade = JOptionPane.showInputDialog("Enter the score of student " + (numStudents[i] + 1) + ":");
Here you are using "numStudents[i] + 1" as default value in array would be 0 so it would always come as 1 instead of this use "i+1"
Re: For Loop NOT Incrementing...
Man do I feel stupid. It worked. Thanks for your help.