我仍然是java的初学者,但我在下面有这个代码我差不多完成了写作,但我很难弄清楚如何计算%10。也出于某种原因,它只需要原始的20000.00和30000.00并显示而不是新的%10增加的工资。怎么做到这一点?感谢您的帮助或见解,谢谢,
public class Employee
{
private String FirstName;
private String LastName;
private double MonthlySalary;
public Employee (String FirstName, String LastName, double MonthlySalary)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.MonthlySalary = MonthlySalary;
}
public void setFirstName (String firstName)
{
this.FirstName = FirstName;
}
public String getFirstName ()
{
return FirstName;
}
public void setLastName (String last)
{
this.LastName = LastName;
}
public String getLastName ()
{
return LastName;
}
public void setMonthlySalary (double salary)
{
if ( MonthlySalary < 0 )
this.MonthlySalary = 0;
else
this.MonthlySalary = MonthlySalary;
}
public double getMonthlySalary ()
{
return MonthlySalary;
}
}
public class EmployeeTest
{
public static void main (String [] args)
{
Employee employee1 = new Employee ( " Bruce ", " Wayne ", 30000.00 );
Employee employee2 = new Employee ( " Clark ", " Kent ", 20000.00);
System.out.println( "Employee1: " + employee1.getFirstName() + employee1.getLastName() );//Displays employee1 name
System.out.println( "Initial Salary: $ " + employee1.getMonthlySalary () );//Displays initial yearly salary
System.out.println( "Employee2: " + employee2.getFirstName () + employee2.getLastName() );//Displays employee2 name
System.out.println( "Initial Salary: $ " + employee2.getMonthlySalary () );//Displays initial yearly salary
System.out.println( "\n\t\t\t********Salary After 10% Raise********\n\t\t\t");//Banner to separate initial salary from 10% raised salary
System.out.println( "Employee1: " + employee1.getFirstName() +employee1.getLastName() );//Displays employee1 name
System.out.println( "Salary After 10% Raise: " + employee1.getMonthlySalary() );//Displays yearly salary after the 10% increase
System.out.println( "Employee2: " + employee2.getFirstName() +employee2.getLastName() );//Displays employee2 name
System.out.println( "Salary After 10% Raise: " + employee2.getMonthlySalary() );//Displays yearly salary after the 10% increase
}
}
答案 0 :(得分:1)
在打印增加的工资之前,您应该设置新值。 为此,您必须修改setMonthlySalary方法,如下所示:
public void setMonthlySalary (double salary) {
if ( salary < 0 )
this.MonthlySalary = 0;
else
this.MonthlySalary = salary;
}
并做
employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.1L);
employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.1L);
答案 1 :(得分:0)
你没有在10%加注后定义monthlySalary - 为monthlySalaryAfter10创建一个值并将monthlySalary乘以1.1
答案 2 :(得分:-1)
假设你有一个值 5 并且你想将它增加 10% 这意味着你应该知道你的值是 0.5 的 10% 是多少,然后你将这个值和 10% 的值添加到你增加的价值 此过程类似于将您的值乘以 110% 所以第一个过程: 5 * .1 = .5 5 + .5 = 5.5
这需要很多步骤 这是您获得 110% 价值的第二个过程
5 * 110/100 = 5 * 1.1 = 5.5