c# - 已分配变量,但从不使用其值

时间:2016-01-25 00:58:21

标签: c#

private void btnHasil_Click(object sender, EventArgs e)
    {
        int rataipa = Convert.ToInt32(NIPA.Text);
        int uipa, uipa1, uipa2, uipa3, uipa4;

        if ((rataipa >= 61) && (rataipa <= 69))
        {
            uipa1 = (70 - rataipa) / 10;
            uipa2 = (rataipa - 60) / 12;
        }
        else if ((rataipa == 60) && (rataipa >= 70) && (rataipa <= 72))
        {
            uipa2 = (rataipa - 60) / 12;
        }
        else if ((rataipa >= 76) && (rataipa <= 84))
        {
            uipa3 = (85 - rataipa) / 13;
            uipa4 = (rataipa - 75) / 10;
        }
        else if ((rataipa >= 72) && (rataipa <= 75) || (rataipa == 85))
        {
            uipa3 = (85 - rataipa) / 13;
        }
        else
            uipa = 1;
    }

我尝试在变量uipa中构建错误显示,并显示变量&ubea&#39;被分配,但它的价值从未使用,但我之后使用它,现在我不知道如何解决。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您在代码的最后指定uipa,但之后您实际上从未对其进行任何操作。

else uipa = 1;

要删除警告,您需要对uipa执行某些操作或完全删除上述语句。就编译器而言,uipa没有用处。

如果您稍后决定使用uipaif语句,请确保将uipa初始化为某些默认值,否则您将运行关于单位变量的不同警告。

int uipa = 0, ....;  // initialise uipa here to a default value
.
.
.
else uipa = 1;

if (uipa ==1) // this will solve the variable is assigned but never used problem
{
    // do something 
}

答案 1 :(得分:-2)

int rataipa = Convert.ToInt32(NIPA.Text);
    int uipa, uipa1, uipa2, uipa3, uipa4;
    if ((rataipa >= 61) && (rataipa <= 69))
    {
        uipa1 = (70 - rataipa) / 10;
        uipa2 = (rataipa - 60) / 12;
    }
    else if ((rataipa == 60) && (rataipa >= 70) && (rataipa <= 72))
    { uipa2 = (rataipa - 60) / 12; }
    else if ((rataipa >= 76) && (rataipa <= 84))
    {
        uipa3 = (85 - rataipa) / 13;
        uipa4 = (rataipa - 75) / 10;
    }
    else if ((rataipa >= 72) && (rataipa <= 75) || (rataipa == 85))
    { uipa3 = (85 - rataipa) / 13; }
    else 
    {         //add this bracket
     uipa = 1;
    }