mercredi 2 octobre 2019

c# if statement redundancy

I'm new to C# and making a simple grade calculator as a way to learn. I have a 6 text boxes for a user to enter numbers and then a letter grade will show up beside it. It all works fine but it seems very redundant as I have 6 grades to convert and have 5 if statements for each just to show letters and then another one for average grade. I assume there is a way to make this less redundant

if (grade1 >= 80)
 {
    labelGrade1.Text = "A";
 }
else if (grade1 >= 70 && grade1 <= 79)
 {
    labelGrade1.Text = "B";
 }
if (grade2 >= 80)
 {
    labelGrade2.Text = "A";
 }
    else if (grade2 >= 70 && grade2 <= 79)
 {
    labelGrade2.Text = "B";
 }

if (grade1 >= 80)
 {
    labelGrade1.Text = "A";
 }
    else if (grade1 >= 70 && grade1 <= 79)
 {
    labelGrade1.Text = "B";
 }
if (grade2 >= 80)
 {
    labelGrade2.Text = "A";
 }
    else if (grade2 >= 70 && grade2 <= 79)
 {
    labelGrade2.Text = "B";
 }

Aucun commentaire:

Enregistrer un commentaire