Delphi: 类型不兼容:'integer' 和 'extended'

8

我需要编写一个程序来计算你工作的小时数所得到的支付金额。

以下是代码:

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作业。 非常感谢任何帮助! 提前致谢!

4
在你上一个问题中,我说过:“当你呈现一个错误信息时,请确保我们能够将信息中的行号与你呈现的代码匹配起来。”请务必注意这个建议。 - David Heffernan
1个回答

16

错误在这里:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接