2014-02-05 34 views
6

我需要制定一個程序,計算出您在工作時間內獲得的付款金額。 下面的代碼:德爾福:不兼容的類型:'整數'和'擴展'

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' 

請你注意:我是新手用戶的編碼一起,這是它的功課。 任何幫助將不勝感激! 在此先感謝!

+4

在你最後一個問題上我說:*當你提出一個錯誤信息時,確保我們可以用你提供的代碼與消息中的行號結婚*請注意這個建議。 –

回答

12

錯誤是在這裏:

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

請仔細閱讀它。雖然給出的例子並不完全符合你的情況,但它非常接近。編譯器錯誤不是可怕的事情。它們帶有描述性文本,您可以通過閱讀並解決您的代碼如何導致特定錯誤來解決您的問題。

+0

謝謝!再次:第 –