-1

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Serdyna
  • 19
  • 2
  • you need to reset sum and average to 0 after each loop – Icarus Sep 09 '22 at 08:09
  • Welcome. You should take a look at [ask] and its linked resources. The title should contain a brief summary of the question. – HoRn Sep 10 '22 at 03:38

1 Answers1

0

Try to change next() to nextLine(). Also, add a blank nextInt after taking the name as it might transfer the enter key to nextInt.

Also, move average outside the for loop

  • 1
    and then expect: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/16320675) – user16320675 Sep 09 '22 at 08:05
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 14 '22 at 01:48