加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:26:44 所属栏目:大数据 来源:网络整理
导读:我需要制定一个计划,计算出你工作时间会得到的金额. 这是代码: unit HoursWorked_u;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,ExtCtrls,StdCtrls,Spin;type TForm1 = class(TForm) lblName: TLabel; edtN
我需要制定一个计划,计算出你工作时间会得到的金额.
这是代码:
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功课.
任何帮助将非常感激!
提前致谢!

解决方法

错误在这里:
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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读