我有以下内容:
string compararcom610b;
//Variables.Gcombinadofavdesf is defined in another class
if (something)
{
}
else if (Variables.Gcombinadofavdesf == 1)
{
//do stuff_1
}
else if (compararcom610b == "1" && Variables.Gcombinadofavdesf == 1)
{
//do stuff_2
}
假设在正确的时刻compararcom610b = 1
和Variables.Gcombinadofavdef = 1
,
是stuff_1
还是stuff_2
吗?
或者只会stuff_1
完成?
答案 0 :(得分:2)
由于您在进入某个区块后立即使用else if
,因此会跳过其他区域。
它只会执行第1项。
如果你想要两者都被执行使用:
if (something)
{
}
else if (Variables.Gcombinadofavdesf == 1)
{
//do stuff_1
}
if (compararcom610b == "1" && Variables.Gcombinadofavdesf == 1)
{
//do stuff_2
}
答案 1 :(得分:0)
如果Variables.Gcombinadofavdesf == 1,则执行stuff_1。
否则[else]如果compararcom610b ==" 1"和Variables.Gcombinadofavdesf == 1,做stuff_2。
这更有意义吗?
第二个条件是第二个条件,如果,再次检查Variables.Gcombinadofavdesf == 1的那个是多余的。如果Variables.Gcombinadofavdesf == 1,则首先检查其值将导致第二个else - 如果被跳过。如果它是假的,那么第二次检查也将是假的。
在if else-if else-if ... else语句组合中,只会执行一个。您可以考虑每个额外的其他 - 如果作为备份计划,如果它失败之前的if。