[英]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.