简体   繁体   中英

How do i make average grade refer to grade which i created in If-Else Statement

public static boolean gradeCalculator() {
    double mark,student;
    String grade = "";
    double total = 0;

    System.out.println("Enter number of students :");
    student = scanner.nextDouble();

    for(int i=0; i<student; i++){

    System.out.println("Enter mark for student "+ (i+1) +" : ");
    mark = scanner.nextDouble();
    total = total + mark;
    double average = total/student;
    {
      if(mark >=0 && mark <=39){
        grade = "F" ; 

         }
      else if(mark >=40 && mark <=49){
        grade = "P";
         }
      else if(mark >=50 && mark <=59){
        grade = "D";
        }
      else if(mark >=60 && mark <=69){
        grade = "C";
         }
      else if(mark >=70 && mark <=79){
        grade = "B";
         }
      else if(mark >=80 && mark <100){             
        grade = "A";
        }  
      System.out.println( "Grade : " +grade);
    }
}
    System.out.println( "Average Mark : " +total/student);
    System.out.println( "Average Grade : " );
    System.exit(0);
    return false;
}

I wanted to calculate the grade for students, and the average at the end of the script. But i'm wondering how do i make the average mark refer to the grade which i created previously in the if-else statement.

I'd suggest saving all grades in some collection. Since grade is set in a loop, you don't have access to any value it had but the last one after the loop. These values are overwritten because this same variable is set to another value the next time the loop is executed.

I recommend to make grade an enum :

public enum Grade {A, B, C, D, E, F};
Grade grade;

One advantage is that this makes clear what the possible values are. Another advantage is that each enum value has an integer associated with it, allowing you to to calculations like averaging.

You get the integer value from the enum like this:

int gradeAsInt = grade.ordinal();

In the loop, calculate the sum of these integers by doing this in each iteration:

sumOfGrades += grade.ordinal();

After the loop divide the sum by the total number and convert it back to an enum (add 0.5 for rounding correctly):

Grade averageGrade = Grade.values[(sumOfGrades + 0.5) / student];

Of course, you should do range checking first.

I think there is different problems there,+ First, if you need to add a new to add a new or delete a grade, you would need to modify the if-else , which does not follow the Open/Closed Principle. Moreover, the knowledge about the grades is disseminated for each if-else sentence and it retrieves as a simple String ( "F" , "P" , "D" ,...). Imagine that you need will require to manage the grade later, you will need to use again another if-else and .equals("F") , .equals("P") . Then, imagine the you will need to rename the grade "F" by "X" . Then, you will need to find all the "F" and write "X" , of course your IDE will be very useful for this task, but it is not a good practice.

I think about the grade as a list of constants that represent and state, so I think you should take a look to the enumerates . It would be interesting for you because it will allow ranges to be represented and managed in a simple way.

Take a look to the enumerates java link where a Planet example is developed. However, it could be too elaborate for your case, so take a look to this answer . There you will find a enumerate, Wealth , that describes a range. So, I think it could be interesting for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM