I want the program to prompt the user in entering their grade and the unit for that subject. Using nested for loops, I wished to simultaneously get this 2 input simultaneously per subject:
Scanner userInput = new Scanner(System.in);
//Initializing variables and array
double[] userGrades = new double[10];
double[] userUnits = new double[10];
int subjectCounter = 1;
// Nested for Loop to get each grade and units per subject
for(int i = 0; i < userGrades.length; ++i){
System.out.println("\nEnter your grade for Subject " + subjectCounter + ": ");
userGrades[i] = userInput.nextDouble();
subjectCounter++;
for(int j = 0; j <= i; ++j){
System.out.println("Enter the number of units:");
userUnits[j] = userInput.nextDouble();
}
}
The output for this prompt is:
Enter Grade for Subject 1:
Enter Unit for Subject 1:
Enter Grade for Subject 2:
Enter Unit for Subject 2:
...
When I check the elements of userGrades[], it outputs the grades of every subject which is what I want. But when I check the elements of userUnits[], it outputs 9 zeroes, and the last number I typed for "Enter Units for this Subject: " (which is in Subject 10).
Why do userGrades[] store the correct elements while userUnits[] does not? I need help as I just started learning nested for loops and arrays.