Shahzada Shahzada - 2 months ago 6
C# Question

how to display grade?

Hi Guys Im Trying to write a program to enter a students grade .Their mark should be between 0 to 100. The program should display:
a. "note is A" when the score is 90 and more
b. "note is B" when the note is of 80 or more but less than 90
c. "note is C" when the score is 70 and more but less than 80
d. "note is D" when the score is 60 and more but less than 70
e. 'Failure' when the score is less than 60
f. If the input value is less than 0 or greater than 100 the program should display "Enter a value between 0 and 100"
The program must loop continuously
The problem is when im running the code it is asking the user to enter the grade and but it does not go through my if and else to check what is going to be note .

private static void note()
{
int[] test = new int[1];


for (int i = 0; i <= test.Length; i++)
{
Console.WriteLine("Please enter test " + i);

Console.ReadLine();


if ( i<=90)
{ Console.WriteLine("note is A");

}

else if (i <=80 || i > 90 )
{
Console.WriteLine("note is B");
}

else if (i <= 70 || i > 80)
{
Console.WriteLine("note is C");
}

else if (i <= 60 || i > 70)
{
Console.WriteLine("note is D");
}

else if (i >60)
{
Console.WriteLine("Failure");
}

else if (i > 0 || i < 100)
{
Console.WriteLine("Enter a value between 0 and 100");
}
else if (i == 999)
{ Console.WriteLine("you Enterd 999 to stop ");

}
test[i] = Console.Read();
Console.ReadKey();


}

Answer

The way you made you're code now, it's a wonder if it gives an answer at all. You should make it like this:

private static void note()
{
    int[] i = new int[1];

        Console.WriteLine("Please enter test result");

        i[0]= Convert.ToInt32(Console.ReadLine());


        if ( i[0]>=90)
        { 
           Console.WriteLine("note is A");
        }

        else if (i[0] >=80 && i[0] < 90 )
        {
            Console.WriteLine("note is B");
        }

        else if (i[0] >= 70 && i[0] < 80)
        {
            Console.WriteLine("note is C");
        }

        else if (i[0] >= 60 && i[0] < 70)
        {
            Console.WriteLine("note is D");
        }

        else if (i[0] < 60)
        {
            Console.WriteLine("Failure");
        }

        else if (i[0] < 0 || i[0] > 100)
        {
            Console.WriteLine("Enter a value between 0 and 100");
        }
        else if (i[0] == 999)
        { Console.WriteLine("You enterd 999 to stop ");

        }
        Console.Writeline("You're result is:" + i[0]);
   Console.ReadKey();
    }

An array of 1 is not very useful. Just make it an int. And make sure to learn the difference between <, >, >=, <= and between ||, &&