当前位置:编程学习 > Delphi >>

DELPHI程序,小问题

为什么选择“出发”输入除数“0”,不会弹出SHOWMESSAGE对话框!

代码如下:

procedure TForm1.Button1Click(Sender: TObject);   //计算按钮
var
  x,y,z :Integer;
 
begin
  x :=StrToInt(edt1.Text);
  y :=StrToInt(edt2.Text);
 case RadioGroup1.XItemIndex of                                                 X
  0: z := x+y;
  1: z := x-y;
  2: z := x * y;
  3: z := x div y;
  end;                       //////  Case end  ////////
  edt3.Text :=IntToStr(z);
  if  RadioGroup1.ItemIndex=3 then
  begin
    if strtoint(edt2.Text)=0 then
    showmessage('被除数不能为零')
  end
  else
  begin
    if checkbox2.Checked then
    edt3.Text := edt3.Text +'余'+inttostr(x mod y);
  end;
end;

答案:

 if  RadioGroup1.ItemIndex=3 then
  begin
    if strtoint(edt2.Text)=0 then
    showmessage('被除数不能为零')
  end
  else
  begin
    if checkbox2.Checked then
    edt3.Text := edt3.Text +'余'+inttostr(x mod y);
  end;

case RadioGroup1.XItemIndex of  X
  0: z := x+y;
  1: z := x-y;
  2: z := x * y;
  3: z := x div y;
  end;   

把这2个地方变换下位置 .

 

因为当你选择除法的时候 先执行了  case RadioGroup1.XItemIndex of  X

3: z := x div y; 这个时候 已经异常了  程序中止!! 所以先判断 ,后执行

顺序换了!

  case RadioGroup1.XItemIndex of  X

3: z := x div y;

上一个:Delphi小数位修约问题
下一个:怎样学习delphi程序设计?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,