Reputation: 49
What am I doing wrong exactly? I am not certain if the "&&" in the if conditions is what is causing the issue or if it is something else. I get a red mark underneath the ">=" part of the condition in every single "if". Can someone tell me how to remedy the issue?
static void Main(string[] args)
{
Int32 pointsEarned;
string firstName;
string lastName;
Int32 percentageGrade;
Console.WriteLine("Enter the student's first name.");
firstName = Console.ReadLine();
Console.WriteLine("Enter the student's last name.");
lastName = Console.ReadLine();
Console.WriteLine("Enter the amount of points earned.");
pointsEarned = int.Parse(Console.ReadLine());
percentageGrade = pointsEarned / 1000;
if (percentageGrade <=100 && >= 90);
{
string finalGrade = "A";
}
else if (percentageGrade <=89 && >= 80 );
{
string finalGrade = "B";
}
else if (percentageGrade <= 79 && >= 70);
{
string finalGrade = "C";
}
else if (percentageGrade <= 69 && >= 60);
{
string finalGrade = "D";
}
else if (percentageGrade <= 59 && >= 0);
{
string finalGrade = "F";
}
}
}
}
Upvotes: 1
Views: 74
Reputation: 10718
While it makes sense in english to say "if value is greater than x or less than y", this does not make sense to code - the compiler is very bad at inferring your use of the same variable in cases like these.
Instead, you have to reprint the variable you're testing against:
if(percentageGrade <= 100 && percentageGrade >= 90)
Similarly, while we can write "min < x < max" in most forms of math, this will not makes sense to C#, because it can only perform operations with two inputs. Thus, the first operator will look at min
and x
and output a bool, but the second <
will look at that bool and max
, thus producing an error.
Also, as pointed out by Travis, don't use ;
after conditions - just as method declarations don't need them, block declarations don't either. Be wary, however, as this creates a bizarre subtlety and is not going to throw a compilation error EDIT On a single if
block - it'll still throw an error on else
since it's no longer paired with an if
Upvotes: 3
Reputation: 117175
I'm not posting this as a direct answer to the question, but I thought it might be worthwhile posting a more robust solution than using a series of if-statements.
The problem with if-statements like this is that you can easily make logical coding mistake. Something like this:
else if (percentageGrade <= 59 && percentageGrade>= 60)
{
finalGrade = "D";
}
Note the if is miss-typed and can never be true. This kind of error is easy to make.
Here's an alternative that should be a little more robust for you.
var grades = new []
{
new { grade = "A", cutoff = 90 },
new { grade = "B", cutoff = 80 },
new { grade = "C", cutoff = 70 },
new { grade = "D", cutoff = 60 },
new { grade = "F", cutoff = 0 },
};
string finalGrade =
grades
.Where(g => percentageGrade >= g.cutoff)
.Select(g => g.grade)
.First();
This creates a "table" of grades and their cutoff scores. The .Where
clause filters the list for only those grades where the student made the cutoff. So a score of 75
would only include "C"
, "D"
, & "F"
. The .Select
selects the actual grade, and the .First
picks the first grade that the student hit the cutoff for. So a score of 75
would have a grade of "C"
.
Upvotes: 1
Reputation: 1
i have rectified the code:
static void Main(string[] args)
{
Int32 pointsEarned;
string firstName;
string lastName;
Int32 percentageGrade;
Console.WriteLine("Enter the student's first name.");
firstName = Console.ReadLine();
Console.WriteLine("Enter the student's last name.");
lastName = Console.ReadLine();
Console.WriteLine("Enter the amount of points earned.");
pointsEarned = int.Parse(Console.ReadLine());
percentageGrade = pointsEarned / 1000;
if (percentageGrade <=100 && percentageGrade>= 90)
{
string finalGrade = "A";
}
else if (percentageGrade <=89 && percentageGrade>= 80 )
{
string finalGrade = "B";
}
else if (percentageGrade <= 79 && percentageGrade>= 70)
{
string finalGrade = "C";
}
else if (percentageGrade <= 69 && percentageGrade>= 60)
{
string finalGrade = "D";
}
else if (percentageGrade <= 59 && percentageGrade >= 0)
{
string finalGrade = "F";
}
}
the errors were: you have used ";" in every if statement, which is wrong. apart from that, you have to use variable name for another condition after && in if statement. hop, my reply will be useful to you.
Upvotes: 0
Reputation: 854
Remove the semi-colons from the condition lines, and providing both sides of the inequality:
if (percentageGrade <=100 && percentageGrade >= 90)
Upvotes: 1