gpt4 book ai didi

Java:计算员工工资

转载 作者:行者123 更新时间:2023-12-01 07:36:37 25 4
gpt4 key购买 nike

我又遇到了一个关于如何显示计算出的员工工资的问题。当我输入时薪时,毛工资不会显示。

这是我到目前为止所做的事情..

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 );
}
}

员工.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();

}
}

最佳答案

我确定您的意思是:

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

还有一件事

double rate = input.nextInt();

应该是

double rate = input.nextDouble(); 

如果您确实期望得到一个实数。

关于Java:计算员工工资,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11114414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com