如何在不返回任何值的情况下创建方法

时间:2022-11-24 12:11:18

I'm trying to create a method where I can pass a float, evaluate it, then update a text label accordingly. Would someone be kind enough to take a look at my code and point me in the right direction? Many thanks in advance...

我正在尝试创建一个方法,我可以传递一个浮点数,计算它,然后相应地更新文本标签。有人会友好地看看我的代码并指出我正确的方向吗?提前谢谢了...

public static GetGrade(float wp)
    {
        if (wp >= 100)
        {
            grade_current.Text = "A";
        }
        else if (wp >= 90)
        {
            grade_current.Text = "A";
        }
        else if (wp >= 75 && wp <= 89)
        {
            grade_current.Text = "B";
        }
        else if (wp >= 60 && wp <= 74)
        {
            grade_current.Text = "C";
        }
        else if (wp >= 50 && wp <= 59)
        {
            grade_current.Text = "D";
        }
        else
        {
            grade_current.Text = "F";
        }
    }

I'm trying to call the method with GetGrade(wp);

我试图用GetGrade(wp)调用该方法;

2 个解决方案

#1


3  

Just use 'void', also you can clean up the code a bit to make it easier on the eyes:

只需使用'void',你也可以稍微清理代码,使眼睛更容易:

public static void GetGrade(float wp)
{
    if (wp >= 100)
        grade_current.Text = "A";
    else if (wp >= 90)
        grade_current.Text = "A";
    else if (wp >= 75)
        grade_current.Text = "B";
    else if (wp >= 60)
        grade_current.Text = "C";
    else if (wp >= 50)
        grade_current.Text = "D";
    else
        grade_current.Text = "F";
}

#2


2  

Your method is missing a return type. If you don't need to return anything then just use "void".

您的方法缺少返回类型。如果您不需要返回任何内容,那么只需使用“void”即可。

public static GetGrade(float wp)

=>

public static void GetGrade(float wp)

#1


3  

Just use 'void', also you can clean up the code a bit to make it easier on the eyes:

只需使用'void',你也可以稍微清理代码,使眼睛更容易:

public static void GetGrade(float wp)
{
    if (wp >= 100)
        grade_current.Text = "A";
    else if (wp >= 90)
        grade_current.Text = "A";
    else if (wp >= 75)
        grade_current.Text = "B";
    else if (wp >= 60)
        grade_current.Text = "C";
    else if (wp >= 50)
        grade_current.Text = "D";
    else
        grade_current.Text = "F";
}

#2


2  

Your method is missing a return type. If you don't need to return anything then just use "void".

您的方法缺少返回类型。如果您不需要返回任何内容,那么只需使用“void”即可。

public static GetGrade(float wp)

=>

public static void GetGrade(float wp)