我正在尝试创建一个字母评分计算器,但一直收到错误

时间:2020-07-22 19:50:48

标签: c# xamarin

这是我拥有的代码; 我的物品是:

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

2 个答案:

答案 0 :(得分:1)

看这行:

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

您不能将int gradepct = 110;这样的语句放在条件的中间。另外,您应该在if语句后使用方括号。将其重写为:

if

但这实际上也不起作用-您制作了int gradepct = 110; if (testScore >= 0 && testScore <=100) { .... } 类型的gradepct,但是您正在尝试为其分配一个int,例如:

string

此外,还不清楚您为什么首先要为其分配gradepct = "C"; -总是在您的110语句过程中进行设置,因此您实际上并不需要需要将其设置为特别的任何内容。

您应该改为执行以下操作:

if

此外,请检查您是否确实在任何地方声明了string gradepct = null; if (testScore >= 0 && testScore <=100) { ... } 。 (如果这样做,则程序中可能会出现另一个编译错误。)

答案 1 :(得分:1)

您可以将testScore设置为方法的参数。并且每次该方法获得一个评分时,您都会在测试方法中获得gradepct。

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