简体   繁体   中英

I'm trying to create a letter grade calculator but keep receiving errors

This the code that I have; The items that I have is:

string gradepct = null;
int testScore = 100;
if (testScore <= 60) { gradepct = "F";}
else if (testScore < 69) {gradepct = "D";}
else if (testScore < 79) {gradepct = "C";}
else if (testScore < 89) {gradepct = "B";
} else {gradepct = "A";}
Console.WriteLine ("You have recived the grade of gradepct!");
Console.WriteLine ("Please enter a correct grade")

Look at this line:

if (testScore >= 0 &&int gradepct = 110;testScore <=100)

You can't put int gradepct = 110; in the middle of the condition for your if statement like this. Also, you should use brackets after the if statement. Rewrite it to this:

int gradepct = 110;
if (testScore >= 0 && testScore <=100)
{
....
}

That actually won't work either, though - you made gradepct of type int , but you're trying to assign a string to it, eg:

gradepct = "C";

Also, it's a little unclear why you're assigning 110 to it in the first place - it'll always be set in the course of your if statements, so you really don't need to set it to anything in particular.

You should do the following instead:

string gradepct = null;
if (testScore >= 0 && testScore <=100)
{
...
}

Also, please check to see whether you did, in fact, declare testScore anywhere. (If you did, you likely have another compile error in the program).

You can set the testScore as a parameter of the method. And every time the method received a grade, you will get the gradepct in test method.

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();


        Console.WriteLine("Please enter a correct grade");

        test(69);
        test(71);
        test(55);

    }

    public void test(int testScore) {

        string gradepct = "";

        if (testScore <= 60) { gradepct = "F"; }
        else if (testScore < 69) { gradepct = "D"; }
        else if (testScore < 79) { gradepct = "C"; }
        else if (testScore < 89)
        {
            gradepct = "B";
        }
        else { gradepct = "A"; }

        Console.WriteLine("You have recived the grade of gradepct:" + gradepct );
    }
}

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