My program to calculate grades and their average from teachers input, only properly counts the first input of grades. Any other input becomes a number over 100. I don't really know what the problem is here.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class programmingProjectGrades {
public static void main(String[] args) throws IOException
{
Scanner scn = new Scanner(System.in);
PrintWriter out = new PrintWriter("grades.txt");
double sum = 0;
double average = 0;
int studentsGrades = 0;
String letterGrade;
System.out.println("Please enter the number of students you wish to enter: ");
int numberOfStudents = scn.nextInt();
for(int i = 0; i < numberOfStudents; i++){
System.out.println("Student's Name: " );
String students_name = scn.next();
System.out.println("Enter 5 test scores for this student : ");
for (int x = 0;x < 5; x++)
{
studentsGrades = scn.nextInt();
sum += studentsGrades;
average = sum /5;
}
if (average >= 90){
letterGrade = "A";}
else if (average >= 80){
letterGrade = "B";}
else if (average >= 70){
letterGrade = "C";}
else if (average >= 60){
letterGrade = "D";}
else {
letterGrade = "F";}
out.println(students_name);
out.println("Final Grade: " + average);
out.println("Letter Grade: " + letterGrade);
}
out.close();
}
}
I expected to see average grade calculated for each student by using loops of for and if statements, although only the first input works properly. I cannot find where the problem is.