2012-06-20 92 views
1

我再次有關於如何顯示員工.. 當我在每小時的速度型,薪資總額將不顯示的工資計算上有問題..的Java:計算員工工資

這裏是我這樣做的遠..

的WageCalcu.java

public class WageCalcu 
    { 
     private String employeeName; 
     private int hours; 
     private double rate, pay; 

     public void setEmployeeName (String name) 
     { 
      employeeName = name; 
     } 
     public String getEmployeeName() 
     { 
      return employeeName; 
     } 
     public double calculatePay(int hours, double rate) 
     { 
      if (hours > 40) 
      { 
       int extraHours = hours - 40; 
       pay = (40 * rate) + (extraHours * rate); 
      } 
      else pay = hours * rate; 

      return pay; 
     } 
     public void displayEmployee() 
     { 
      System.out.printf("Employee's name: %s", getEmployeeName()); 
      System.out.printf("\nGross Salary: ", + pay); 
     } 
    } 

的Employee.java

import java.util.Scanner; 
    public class Employee 
    {  
     public static void main(String[] args) 
     { 
      Scanner input = new Scanner(System.in); 
      WageCalcu employee = new WageCalcu(); 

      System.out.print("Enter Employee %s name: "); 
      String name = input.nextLine(); 
      employee.setEmployeeName(name); 

      System.out.print("Enter how many hours worked: "); 
      int hours = input.nextInt(); 

      System.out.print("Enter hourly rate: "); 
      double rate = input.nextInt(); 

      employee.calculatePay(hours, rate); 
      employee.displayEmployee(); 

      System.out.println(); 

     } 
    } 
+0

請使用代碼塊一致性和邏輯縮進。這有助於讀者。你不會想讓我們更難以幫助,對嗎? –

+0

對於工資計算,您應該**使用BigDecimal而不是double。 – jpangamarca

回答

7

我確定你的意思是:

System.out.printf(「\ nGross Salary:%f」,pay);

一件事

double rate = input.nextInt(); 

應該

double rate = input.nextDouble(); 

如果你真的希望一個實數。

+0

謝謝..我明白了.. :) – drayl

0

你已經錯過了printf("\nGross Salary: ", + pay);

0

%s的我會放:System.out.printf("\nGross Salary: %.2f", pay);顯示2位小數。

+0

這不會提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/17171295) – Sergey

0

這次派對遲了五年,但我會講我的作品。

你已經設置它來確定一個員工有多少加班時間,但是你沒有計算他們的加班工資。

你有什麼:

pay = (40 * rate) + (extraHours * rate); 

它應該是什麼:

pay = (40 * rate) + (extraHours * rate * 1.5);