Click here to Skip to main content
15,884,980 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to write a Java program to compute the grades of many students. The program
should read the number of students then, for each student, read 3 scores. i did wrote a code to read from user to calculate for a single student.

What I have tried:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Score of TMA :");
        double TMA = input.nextDouble();
        System.out.println("Enter Score of MTA :");
        double MTA = input.nextDouble();
        System.out.println("Enter final exam score :");
        double exam = input.nextDouble();
        String grd=theGrads(TMA,MTA,exam);
        System.out.println("the grade is: " + grd);

    }

    public static String theGrads  (double TMA,double MTA , double exam) {
        double total =TMA + MTA + exam;

        String grade = "";

        if(total>=90)
            grade="A";
        else if(total >=80 && total <90)
            grade="B";
        else if(total >=70 && total <80)
            grade="C";
        else if(total >=60 && total <70)
            grade="D";
        else
            grade="F";

        return grade;

    }
}
Posted
Updated 23-Oct-21 8:27am
Comments
Patrice T 23-Oct-21 13:57pm    
Did you read the documentation about 'loops' ?
What you don't understand ?
ventus sol 23-Oct-21 14:19pm    
i tried to read from user number of students but dont know how
gggustafson 23-Oct-21 14:38pm    
First, "of many students" suggests that you need to loop over input until you've read all the data. Second, if there is a need to keep the values for later use, then you need to save the data in some data structure; I assume there is no such need.

Pseudo-code for the program is:
    read number_of_students
    for each student
        {
        read the 3 scores
        compute the letter grade
        print the results
        }

The for loop would expand to
    for ( int i = 0; ( i < number_of_students ); i++ )

I strongly suggest that you write three methods:
    read_scores ( )
    compute_letter_grades ( )
    print_letter_grade ( )

Your method "theGrads" should be renamed "compute_letter_grades". Also why are you using double rather than float or integer. Is there a need for such precision?

1 solution

You will need a collection to keep "many students" in - and array or List will do (depending on what your teacher has been talking about recently - use that).

Then you will need a loop: Loops in Java | Java For Loop - Javatpoint[^] explains them.
 
Share this answer
 
Comments
gggustafson 23-Oct-21 14:41pm    
Only if OP needs to keep the values for later use. Seeing the trivial nature of the problem, I don't think OP is ready for data structures.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900