注册 登录
编程论坛 Delphi论坛

If语句

sheng443 发布于 2008-10-23 05:52, 1525 次点击
两个不同的结果,为什么????求救~~~~
aihao:='';
if checkbox1.Checked then
if aihao='' then
aihao:='文学'
else
aihao:=aihao+'、'+'文学';
if checkbox2.Checked then
if aihao='' then
aihao:='绘画'
else
aihao:=aihao+'、'+'绘画';
if checkbox3.Checked  then
if aihao='' then
aihao:='音乐'
else
aihao:=aihao+'、'+'音乐';
if checkbox4.Checked   then
if aihao='' then
aihao:='体育'
else
aihao:=aihao+'、'+'体育';
if checkbox5.Checked   then
if aihao='' then
aihao:='棋牌'
else
aihao:=aihao+'、'+'棋牌';
if checkbox6.Checked  then
if aihao='' then
aihao:='数学'
else
aihao:=aihao+'、'+'数学';






aihao:='';
if aihao='' then
if checkbox1.Checked then
aihao:='文学'
else
aihao:=aihao+'、'+'文学';
if aihao='' then
if checkbox2.Checked then
aihao:='绘画'
else
aihao:=aihao+'、'+'绘画';
if aihao='' then
if checkbox3.Checked  then
 aihao:='音乐'
 else
 aihao:=aihao+'、'+'音乐';
if aihao='' then
if checkbox4.Checked   then
aihao:='体育'
else
aihao:=aihao+'、'+'体育';
if aihao='' then
if checkbox5.Checked   then
 aihao:='棋牌'
 else
 aihao:=aihao+'、'+'棋牌';
if aihao='' then
if checkbox6.Checked  then
aihao:='数学'
 else
 aihao:=aihao+'、'+'数学';

[[it] 本帖最后由 sheng443 于 2008-10-23 05:53 编辑 [/it]]
6 回复
#2
ruanjian21102008-10-23 09:44
设置断点单步调试运行就知道是什么原因了。。
#3
ruanjian21102008-10-23 09:54
给个截图你,设置断点单步运行调试的,可以很清楚的看到变量aihao的值,你自己试着做一下吧!
只有本站会员才能查看附件,请 登录
#4
nongen2008-10-23 12:07
第一段里面,else所执行的结果checked后的非空
第二段里面是非空后的非checked
#5
sheng4432008-10-23 20:22
谢谢~~~
#6
provoke2008-10-25 14:57
aihao := '';
for i := 0 to ControlCount - 1 do
  if controls[i] is TCheckBox then
    if (controls[i] as TCheckBox).Checked then
      aihao := aiho + '、' + (controls[i] as TCheckBox).Caption;
if aihao <> '' then
  delete(aihao,1,2);
//非空时,删除多余分隔符,注意半角/全角

把各个复选框的标题设为对应的科目名称就行了。

[[it] 本帖最后由 provoke 于 2008-10-25 15:05 编辑 [/it]]
#7
provoke2008-10-25 15:01
if...else 嵌套使用时候记得缩进,尤其是很多if...else 的时候,必要时还要加上begin...end,否则出错时很难找。不推荐用大量的if...else
1