我再次有關於如何顯示員工.. 當我在每小時的速度型,薪資總額將不顯示的工資計算上有問題..的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();
}
}
請使用代碼塊一致性和邏輯縮進。這有助於讀者。你不會想讓我們更難以幫助,對嗎? –
對於工資計算,您應該**使用BigDecimal而不是double。 – jpangamarca