Please help me how can i creat a class and use in windows application for Studen Result card .I want type below code in class and want to use in windows application for student Grade

private string MethodGrade(float Avrage)
        {
            string Grade;

            if (Avrage > 91)
            {
                Grade = "A+";
            }
            else if (Avrage > 81)
            {
                Grade = "A";
            }
            else if (Avrage > 71)
            {
                Grade = "B";
            }
            else if (Avrage > 61)
            {
                Grade = "C";
            }
            else if (Avrage > 51)
            {
                Grade = "D";
            }
            else
            {
                Grade = "F";
            }

            return Grade;
        }

Recommended Answers

All 8 Replies

ddanbe this is very use full for beginner of c#
Thanks

First of all, I would suggest using double instead of float because double can easily accept decimal point and when getting and Average it's not likely that the average will not have a decimal. Here is what I have I think it should work pretty well, I don't really know the grading system well you may have to make a few small changes to the Average values to make sure it falls within the grading system bounds.

public class Grade
        {
            public static string GetGradeAverage(double Average)
            {
                string result = String.Empty;
                if (Average > 94.99)
                {
                    result = "A+";
                }
                else if (Average > 89.99 && Average <= 94.99)
                {
                    result = "A";
                }
                else if (Average > 84.99 && Average <= 89.99)
                {
                    result = "B+";
                }
                else if (Average > 79.99 && Average <= 84.99)
                {
                    result = "B";
                }
                else if (Average > 74.99 && Average <= 79.99)
                {
                    result = "C+";
                }
                else if (Average > 69.99 && Average <= 74.99)
                {
                    result = "C";
                }
                else if (Average > 64.99 && Average <= 69.99)
                {
                    result = "D";
                }
                else
                {
                    result = "F";
                }
                return result;
            }
        }

First of all, I would suggest using double instead of float because double can easily accept decimal point and when getting and Average it's not likely that the average will not have a decimal.

What?

float type has a decimal point, as does decimal type. I don't understand what you are saying here.

Also your conditionals contain unneeded checks. If it isn't > 94.99 (line 6) then it is less than or equal to 94.99 (line 10). You only need to check the first conditional.

Here's some shorter code that does the same thing:

static String Grade(double average) {
    String[] grades = { "A+", "A+", "A", "B+", "B", "C+", "C", "D+", "D" };

    int value = (20 - ((int)average / 5));

    return value >= grades.Length ? "F" : grades[value];
}

What? Lol
Strange post of this CsharpChico guy. Code is even worse then of the thread`˙s creator.

Regardless you still get the same output and it stays close to what the original creator asked for

Here is a Method to get the average of a collection of Grades

public class Grade
        {
            public static double GetGradeAverage(double[] Grades)
            {
                
                int Count = Grades.Length;
                double AddGrade = 0;
              
                for (int i = 0; i < Count; i++)
                {
                    AddGrade += Grades[i];
                }

                return AddGrade / Count;
                
            }
        }

What?

float type has a decimal point, as does decimal type. I don't understand what you are saying here.

Also your conditionals contain unneeded checks. If it isn't > 94.99 (line 6) then it is less than or equal to 94.99 (line 10). You only need to check the first conditional.

I didnt say float didnt except decimal. I said double more easily excepts. The reason i say this is because of being a newb if they dont know that an f has to be placed at end example 94.99f then he will get the error Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type. Had same issue when first started programming, searched for hrs and found snippet for double that solved my issues. wasn't til bout a year later I learned the f trick. Should have explained more to him Sorry for any inconvience or misunderstanding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.