This is part of the code, it gets a line of numbers from a textfile and counts how many numbers fit in each specified range. I'm converting it from if, else if statements (which works fine) just for practice. However none of the numbers are counting except 1, the highest number in the textfile, which fits in the default of this switch. Where did I go wrong?
int i = 0;
switch (students[i].Grade)
{
case 1:
{
if(students[i].Grade <= 59)
distributions[0] += 1;
break;
}
case 2:
{
if(students[i].Grade >= 60 && students[i].Grade <= 69)
distributions[1] += 1;
break;
}
case 3:
{
if(students[i].Grade >= 70 && students[i].Grade <= 79)
distributions[2] += 1;
break;
}
case 4:
{
if (students[i].Grade >= 80 && students[i].Grade <= 89)
distributions[3] += 1;
break;
}
// students with grade of 90 or above
default:
{
distributions[4] += 1;
break;
}
}
Console.WriteLine("0-59: {0}\n60-69: {1}\n70-79: {2}\n80-89: {3}\n90-100: {4}", distributions[0], distributions[1], distributions[2], distributions[3], distributions[4]);
this is the code using if else if statements, works fine.
for (int i = 0; i < students.Length; i++)
if (students[i].Grade <= 59)
{
distributions[0] += 1;
}
else if (students[i].Grade >= 60 && students[i].Grade <= 69)
{
distributions[1] += 1;
}
else if (students[i].Grade >= 70 && students[i].Grade <= 79)
{
distributions[2] += 1;
}
else if (students[i].Grade >= 80 && students[i].Grade <= 89)
{
distributions[3] += 1;
}
//students with grade of 90 or above
else
{
distributions[4] += 1;
}
Console.WriteLine("0-59: {0}\n60-69: {1}\n70-79: {2}\n80-89: {3}\n90-100: {4}", distributions[0], distributions[1], distributions[2], distributions[3], distributions[4]);