Delphi:不兼容的类型:'整数'和'扩展'

时间:2014-02-05 21:28:57

标签: delphi

我需要制作一个计划,计算出你工作时间的工资。 这是代码:

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.

错误消息是:

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

请注意:我是新手用户,所有这些都是IT功课。 任何帮助将非常感激! 提前谢谢!

1 个答案:

答案 0 :(得分:14)

错误在于:

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_-_%27%25s%27_and_%27%25s%27_%28Delphi%29

好好读一读。虽然给出的示例与您的案例不完全匹配,但它非常接近。编译器错误不是害怕的事情。它们带有描述性文本,您可以通过阅读它们并尝试弄清楚代码如何导致特定错误来解决您的问题。