2015-11-05 155 views
-3
import javax.swing.JOptionPane; 

import java.util.Scanner; 

public class pay{ 

static Scanner sc; 

public static void main (String[]args) 

{ 
    double Regpay = 8; 
    double OThour = Regpay * 1.5; 
    double hours = 0; 
    double gpay = Regpay * hours; 
    double Totalpay = OThour + gpay; 

    Scanner input = new Scanner(System.in); 
    System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour"); 
    System.out.println("If workers work more than 40hrs, their pay will be time and a half"); 
    System.out.println("please enter your name"); 
    String name = sc.nextLine(); 

    System.out.println("How many hours did you work this month"); 
    hours = sc.nextDouble(); 

    System.out.println("How many hours overtime did you work"); 
    OThour = sc.nextDouble(); 




    if (hours<=40){ 
     Totalpay = gpay; 
     System.out.print("You are not entitled to overtime pay"); 
    }else{ 
     if (hours>=41) 
     Totalpay = gpay + OThour >=1; 
    } 
    }   
} 

我收到錯誤就行了Totalpay = gpay + OThour >=1;我不知道該怎麼辦,我一直試圖改變周圍的事情,但我不斷收到同樣的錯誤!!!!不兼容類型:布爾不能轉換爲double(錯誤)

+0

你想用這條線完成什麼?看起來你正在比較'gpay + OThour',看它是否大於或等於'1'。 – rgettman

+1

'gpay + OThour> = 1'檢查'gpay + OThour'是否大於或等於1,並返回'true'或'false',由於它不是一個'double'數。 –

回答

0

您正在尋找的答案(我假設)是測試OTHoure是否大於1,並將其添加到TotalPay。要完成此操作,請使用

替換TotalPay = TotalPay + OTHours> = 1
if(OTHours>=1){ 
    TotalPay = gPay + OTHour; 
} 
+0

大部分工作都是由我的老師完成的,但其目的是爲了讓員工支付當月支付的總金額,將時長(OThours)添加到gpay中。目的還在於,如果僱員工作了41小時或更長時間,那麼他應該在工作40小時內每工作小時支付8 x 1.5的費用。 –

0

您正在組合兩個不兼容的表達式類型。

Totalpay = gpay + OThour >=1; 

使用操作的順序, 「gpay + OThour> = 1」 是兩個表達式:

gpay + OThour : (the sum of two doubles) 

(result of gpay + OThour) >= 1 : (a boolean expression) 

這樣做的結果是一個布爾答案(真/假),這是不被解析爲雙倍,所以它不能存儲在TotalPay中。其次,你的結果不會像預期的那樣 - 你使用的變量(gpay)已經被初始化爲0,並且永遠不會被改變。

+0

我應該將0更改爲什麼值? –

0

@purpleorb和@ Jaboyc已經解釋了錯誤被拋出的原因。我要談一個更全面的解決方案。

有幾個問題與此

  1. 從用戶
  2. 布爾被分配到雙重問題(如你所提到的)
  3. 不必要的if語句讓輸入之前查找變量值結構

您可以將代碼更改爲以下來解決這些問題:

Scanner input = new Scanner(System.in); 
    System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour"); 
    System.out.println("If workers work more than 40hrs, their pay will be time and a half"); 
    System.out.println("please enter your name"); 
    String name = input.nextLine(); 

    double hours = 0; 
    System.out.println("How many hours did you work this month"); 
    hours = input.nextDouble(); 

    double Regpay = 8; 
    double OThour = Regpay * 1.5; 
    double gpay = Regpay * hours; 
    double Totalpay = OThour + gpay; 

    if (hours>40){ 
     Totalpay = gpay + (OThour- 1) * (hours - 40); 
    }else{ 
     Totalpay = gpay; 
     System.out.print("You are not entitled to overtime pay"); 
    } 

這樣做是在用戶將值輸入到hours之後進行計算gpay = Regpay * hoursTotalpay = OThour + gpay。此外,這可以通過檢查小時數是否大於40來簡化if語句結構,然後再執行所需的計算。

+0

謝謝你這麼多幫助 –

+0

歡迎你!如果您發現答案有幫助,並回答您的問題,請記住點擊綠色複選標記將其標記爲接受的答案! – Bimde

相關問題