我正在尝试锁定锁定的不同阶段中背景颜色发生变化。我目前正在从标题的最后一个和第二个到最后一个括号中得到错误。
int intLocked = -1;
if (intLocked == 0)
{
this.BackColor = System.Drawing.Color.Red;
}
{
if (intLocked == 1)
}
{
this.BackColor = System.Drawing.Color.Gold;
}
{
if (intLocked == 2)
}
{
this.BackColor = System.Drawing.Color.Green;
}
答案 0 :(得分:1)
您的if语句是用错误的语法编写的。正确的语法是:
if (condition)
{
// do stuff
}
如果要检查变量的多个值,可以执行以下操作:
if (myVar == 0)
{
} else if (myVar == 1)
{
} else if (myVar == 2)
{
}
我想你想要的是这个:
if (intLocked == 0)
{
this.BackColor = System.Drawing.Color.Red;
} else if (intLocked == 1)
{
this.BackColor = System.Drawing.Color.Gold;
} else if (intLocked == 2)
{
this.BackColor = System.Drawing.Color.Green;
}
或者,您可以使用switch语句:
switch (intLocked)
{
case 0:
this.BackColor = System.Drawing.Color.Red;
break;
case 1:
this.BackColor = System.Drawing.Color.Gold;
break;
case 2:
this.BackColor = System.Drawing.Color.Green;
break;
}