簡體   English   中英

Delphi:不兼容的類型:'整數'和'擴展'

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

我需要制定一個計划,計算出你工作時間會得到的金額。 這是代碼:

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

好好讀一讀。 雖然給出的示例與您的案例不完全匹配,但它非常接近。 編譯器錯誤不是害怕的事情。 它們帶有描述性文本,您可以通過閱讀它們並嘗試弄清楚代碼如何導致特定錯誤來解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM