关于如何显示员工的计算工资,我又遇到了一个问题。 当我输入每小时费率时,总薪水将不会显示..
这是我到目前为止所做的......
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 :(得分:7)
我确定你的意思是:
System.out.printf(“\ nGross Salary:%f”,pay);
还有一件事
double rate = input.nextInt();
应该是
double rate = input.nextDouble();
如果你真的想要一个真实的数字。
答案 1 :(得分:0)
您错过了printf( "\nGross Salary: ", + pay );
答案 2 :(得分:0)
我会把:System.out.printf( "\nGross Salary: %.2f", pay);
显示为2位小数。
答案 3 :(得分:0)
这个派对迟到了五年,但我会说出我的作品。
您已将其设置为确定员工加班时间,但您没有计算加班费。
你有什么:
pay = ( 40 * rate ) + ( extraHours * rate );
应该是什么:
pay = ( 40 * rate ) + ( extraHours * rate * 1.5);