简体   繁体   中英

Can't get nested IF's to work properly in C#. What am I doing wrong?

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";
        }


    }
}

}

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

从条件行中删除分号,并提供不等式的两面:

if (percentageGrade <=100 && percentageGrade >= 90)

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" .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM