Delphi:不兼容的类型:'integer'和'extended' [英] Delphi: Incompatible types: 'integer' and 'extended'

查看:1484
本文介绍了Delphi:不兼容的类型:'integer'和'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.

错误msg是:

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

请注意:我是一个新手用户,一起编码,这是IT作业。
任何帮助将不胜感激!
提前感谢!

Please do note: I am a novice user at coding all together and this is IT homework. Any help would be much appreciated! Thanks in advance!

推荐答案

错误在这里:

iHours := sedHours.value * rPay;

右侧是浮点表达式,因为 rPay 是一个浮点变量。您不能将浮点值分配给整数。您需要转换为整数。

The right hand side is a floating point expression because rPay is a floating point variable. 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);

或者您可以使用 Floor 获取最小整数小于或等于浮点值:

Or you might use Floor to get the largest integer less than or equal to the floating point value:

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

或者也许 Ceil ,最小的整数更大超过或等于浮点值:

Or perhaps Ceil, the smallest integer greater than or equal to the floating point value:

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






对于一些更一般的建议,我建议您尝试当您遇到您不明白的错误时查看文档。每个编译器错误都记录在案。以下是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.

这篇关于Delphi:不兼容的类型:'integer'和'extended'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆