delphi if 嵌套问题

我有一个语句,我想在第二层if a>0时在赋值1给b,然后根据b的值来赋值c,不知道if的嵌套是如何的,请问可以这样写吗?beginif a=0 then beginbutton1.clickend else if a>0 then beginb:=1; if b>=0 then c:=0 elsec:=1 end elsebutton2.clickend;更正:这只是一个自己瞎编的例子,具体是要询问,if能够执行if下有if,if之后还有上层if的嵌套吗?具体说,if a...是一个层次,if b...是if a之下的另一个层次。beginif a=0 then beginbutton1.clickend else if (a>0) and (a<10)then beginb:=1; if b>=0 then c:=0 elsec:=1 end elsebutton2.clickend;
2026年09月25日 05:42
有4个网友回答
网友(1):

因为你a的赋值没有变,所以没必要嵌套的这么麻烦:
if a=0 then button1.click;
if (a>0) and (a<10)then
begin
b:=1;
c:=0;
end else
c:=1;
if (a<0) or (a>=10)then button2.click;

网友(2):

begin
if a=0 then
begin
button1.click
end
else if a>0 and a<10 then
begin
b:=1;
if b>=0 then
c:=0
else
c:=1;
end
else
button2.click;
end;
这样写的,不过
b:=1;
if b>=0 then
c:=0
else
c:=1;
这儿有问题,c永远为0

网友(3):

你这个题有问题啊,如果B=1的话,那C永远都是0,得不到其他答案啊。
if (a=0) then
begin
button1.click();
end
else if (a>0) then
begin
b:=1;
if (b>=0) then c:=0 else c:=1;
end
else
begin
button2.click();
end;

网友(4):

感觉象下边的结果
begin
if a=0 then button1.click
else if (a>0)and(a<10)then begin
b:=1;
c:=0;
end else button2.click;
end;