德尔福:不兼容的类型:"整数"和"扩展"



我需要制定一个程序,以算出您工作时间将获得的金额。这是代码:

unit HoursWorked_u;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Spin;
type
  TForm1 = class(TForm)
    lblName: TLabel;
    edtName: TEdit;
    Label1: TLabel;
    sedHours: TSpinEdit;
    btncalc: TButton;
    Panel1: TPanel;
    lblOutput: TLabel;
    Label2: TLabel;
    Panel2: TPanel;
    lblOutPutMonth: TLabel;
    labelrandom: TLabel;
    Label3: TLabel;
    seddays: TSpinEdit;
    procedure btncalcClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
// sedHours and sedDays are SpinEdits
// Rand (R) is South African currency eg: One months work, I will recieve-
// -R10 000.00
// Where R12,50 is paid an hour.
// Need to work out how much I will get paid for how  many hours are worked.
procedure TForm1.btncalcClick(Sender: TObject);
var
    sName                       :string;
    iHours, iDays               :integer;
    rPay                        :real;
begin
  rPay := 12.5;
  sName := edtName.Text;
  iHours := sedHours.value * rPay;
  iDays := sedDays.value * iHours;
    lblOutput.caption := sName + ' You will recieve R' + IntToStr (iHours);
    lblOutputMonth.Caption := 'You will recive R' + intToStr (iDays);
end;
end.

错误msg是:

[Error] HoursWorked_u.pas(51): Incompatible types: 'Integer' and 'Extended'

请注意:我是新手用户一起编码,这就是功课。任何帮助将非常感激!预先感谢!

错误在这里:

iHours := sedHours.value * rPay;

右侧是浮点表达式,因为rPay是浮点变量。您无法为整数分配浮点值。您需要转换为整数。

例如,您可能会四处往最近:

iHours := Round(sedHours.value * rPay);

,或者您可以使用Floor获得最大的整数小于或等于浮点值:

iHours := Floor(sedHours.value * rPay);

Ceil,最小的整数大于或等于浮点值:

iHours := Ceil(sedHours.value * rPay);

有关更多一般建议,我建议您在遇到不了解的错误时尝试查看文档。记录了每个编译器错误。这是E2010不兼容类型的文档:http://docwiki.embarcadero.com/radstudio/en/e2010_incompatible_types_-_types_-_; types_-_%S'_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_AND_;

对它进行了很好的阅读。尽管给出的示例与您的情况并不完全匹配,但它非常接近。编译器错误并不害怕。它们带有描述性文本,您可以通过阅读问题并试图确定您的代码如何导致特定错误来解决问题。

最新更新