繁体   English   中英

Delphi:不兼容的类型:当两个值都分配为实数时,则为“整数”和“扩展”

[英]Delphi: Incompatible types: 'Integer' and 'Extended' when both values are assigned as real

我正在尝试编译一个相当基本的程序来计算BMI,但是我似乎一直在收到此错误,而且我不确定为什么或如何修复它。

这些是我的变量:

weight : real;
height : real;
bmi : real;

我的编码如下:

procedure TForm1.Button1Click(Sender: TObject);
begin
  weight := strtofloat(inputbox('weight', 'Enter your weight in        kilograms',''));
  height := strtofloat(inputbox('height', 'Enter your height in    centimeters',''));
  bmi := weight/sqr(height);
  EDIT1.Text := floattostr(BMI);
end;

我该如何解决该错误,并且是由什么引起的?

Height被误认为是Self.Height ,它是指表单的Height属性,它是一个整数。 为变量使用其他名称,或使其在方法的作用域内是局部的。 以下工作对我来说很好:

procedure TForm1.Button1Click(Sender: TObject);
var
  Weight, Height, BMI: Real;
  s: string;
begin
  s := InputBox('Weight', 'Enter your weight in kilos', '');
  Weight := StrToFloat(s);
  s := InputBox('Height', 'Enter your height in centimeters', '');
  Height := StrToFloat(s);
  BMI := Weight/sqr(Height);
  Edit1.Text := FloatToStr(BMI);
end;

我的首选解决方案是使用其他名称,以避免将来出现任何可能的混乱。 我可能会做更多这样的事情:

procedure TForm1.Button1Click(Sender: TObject);
var
  BodyWeight, BodyHeight, BMI: Real;
  s: string;
begin
  s := InputBox('Weight', 'Enter your weight in kilos', '');
  BodyWeight := StrToFloat(s);
  s := InputBox('Height', 'Enter your height in centimeters', '');
  BodyHeight := StrToFloat(s);
  BMI := BodyWeight/sqr(BodyHeight);
  Edit1.Text := FloatToStr(BMI);
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM