我无法弄清楚如何在代码结束时打印年薪。
我尝试了各种配置,但它最终使它脱离了打印输出。我知道这可能是一个简单的错误,但无法解决。我到底做错了什么?
package AverageSalary;
public class Employee {
//private variables for Employee Name and Monthly Salary.
private String employeeName;
private double monthlySalary;
// Constructor that initializes name and monthly salary
public Employee(String EmployeeName, double mnthSalary) {
employeeName = EmployeeName;
monthlySalary = mnthSalary;
}
//set Employee Name function
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
//get Employee Name Function
public String getEmployeeName() {
return employeeName;
}
//monthlySalary
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
// Method named annualSalary returns the salary for a whole year.
public double annualSalary() {
return getMonthlySalary() * 12;
}
// toString method that returns a string containing the name and monthly
public String toString() {
return String.format("\nEmployee Name: %s, Monthly Salary=$%,.2f",
getEmployeeName(), getMonthlySalary(),
annualSalary());
}
}
答案 0 :(得分:1)
您需要将年薪添加到String
格式;您目前只包括员工姓名和月薪:类似
return String.format("\nEmployee Name: %s, Monthly Salary=$%,.2f, "
+ "Annual Salary=$%,.2f", getEmployeeName(), getMonthlySalary(),
annualSalary());