变量已分配,但从未使用其值
本文关键字:未使用 分配 变量 | 更新日期: 2023-09-27 18:31:38
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 中构建错误显示,它显示变量"UIPA"已分配,但它的值从未使用过,但之后我使用了它,现在我不知道如何修复。 任何人都可以帮忙吗?
你在代码的最后分配uipa
,但你从来没有真正用它做任何事情。
else uipa = 1;
要删除警告,您需要对uipa
执行某些操作或完全删除上述语句。 就编译器而言,uipa
没有任何用处。
如果您决定稍后在 if
语句中使用uipa
,请务必将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
}
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;
}