简体   繁体   English

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

[英]Delphi: Incompatible types: 'integer' and 'extended'

I need to make a program that works out the amount you will get paid for the hours you worked. 我需要制定一个计划,计算出你工作时间会得到的金额。 Here's the code: 这是代码:

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.

The error msg is: 错误消息是:

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

Please do note: I am a novice user at coding all together and this is IT homework. 请注意:我是新手用户,所有这些都是IT功课。 Any help would be much appreciated! 任何帮助将非常感激! Thanks in advance! 提前致谢!

The error is here: 错误在这里:

iHours := sedHours.value * rPay;

The right hand side is a floating point expression because rPay is a floating point variable. 右侧是浮点表达式,因为rPay是浮点变量。 You cannot assign a floating point value to an integer. 您不能将浮点值分配给整数。 You need to convert to an integer. 您需要转换为整数。

For instance, you might round to the nearest: 例如,您可以舍入到最近的:

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

Or you might use Floor to get the largest integer less than or equal to the floating point value: 或者您可以使用Floor来获取小于或等于浮点值的最大整数:

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

Or perhaps Ceil , the smallest integer greater than or equal to the floating point value: 或者Ceil ,大于或等于浮点值的最小整数:

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

For some more general advice I suggest that you try to look in the documentation when you encounter an error that you do not understand. 对于一些更一般的建议,我建议您在遇到您不理解的错误时尝试查看文档。 Every compiler error is documented. 记录每个编译器错误。 Here's the documentation for E2010 Incompatible types: http://docwiki.embarcadero.com/RADStudio/en/E2010_Incompatible_types_-_%27%25s%27_and_%27%25s%27_%28Delphi%29 以下是E2010不兼容类型的文档: http//docwiki.embarcadero.com/RADStudio/en/E2010_Incompatible_types_-_%27%25s%27_and_%27%25s%27_%28Delphi%29

Take a good read of it. 好好读一读。 Although the example given is not an exact match for your case it is very close. 虽然给出的示例与您的案例不完全匹配,但它非常接近。 Compiler errors are not things to be scared of. 编译器错误不是害怕的事情。 They come with descriptive text and you can solve your problem by reading them and trying to work out how your code has led to the particular error. 它们带有描述性文本,您可以通过阅读它们并尝试弄清楚代码如何导致特定错误来解决您的问题。

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

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