我正在编写一个相当简单的程序,只输出2个雇员的姓名,他们的薪水以及该薪水的10%涨幅。
我有2个问题:1)薪水打印为“ $ 0.000000” 2)Ii无法获得加薪方法以使其正常工作

这是我的代码:

public class Employee {

    // instance variable
    private String name;
    private  String lastName;
    private  double salary;

    // setters
    public  String getLastName() { return lastName; }
    public  String getName() { return name; }
    public  double getSalary() { return salary; }

    public  void raise(double raise) { salary = salary + (salary * .1); }

    // getters
    public  Employee(String name, String lastName, double salary) {
        this.name = name;
        this.lastName = lastName;

        if (salary > 0.0) {
          this.salary = salary;
        }
    }
}

public class EmployeeTest {

    public  static void main(String[] args) {

        Employee raise1 = new Employee("Betty", "Jones", 4000.0);
        Employee raise2 = new Employee("Sally", "Mae", 6000.0);

        // Print statements for the employee's name and salary
        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" + "Salary: $%f", raise1.getName(), raise1.getLastName(), raise1.getSalary());

        // THIS IS WHERE I'M HAVNG TROUBLE
        System.out.printf("Her raise will be: %d", raise1.raise(salary));

        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" + "Salary: %f", raise1.getName(), raise1.getLastName(), raise1.getSalary());

        raise2.raise(salary);
    }
}


先谢谢您的帮助!

最佳答案

薪水打印$ 0.00000的原因是因为您出于某种原因放置了if语句,而不仅仅是设置传递给构造函数的薪水。第二件事,void方法不会返回任何内容,这是引发方法无法按您期望的方式工作的原因。我为您修复了问题,请看一下,希望可以帮助您了解您出了错的地方,如果您有任何疑问,请随时向我询问。

员工考试班:

public class EmployeeTest {

    public static void main(String[] args) {

        Employee raise1= new Employee("Betty","Jones",4000.0);
        Employee raise2= new Employee("Sally","Mae",6000.0);

        //Print statements for the employee's name and salary
        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" +
                "Salary: $%.2f",
                raise1.getName(),
                raise1.getLastName(),
                raise1.getSalary());

        System.out.printf("\nHer raise will be: $%.2f",
                raise1.raise(raise1.getSalary()));


        System.out.printf("\n\nEmployee #2\nFirst Name: %s\nLast Name: %s\n\n" +
                "Salary: $%.2f",
                raise2.getName(),
                raise2.getLastName(),
                raise2.getSalary());

        System.out.printf("\nHer raise will be: $%.2f\n",
                raise2.raise(raise2.getSalary()));

    }
}


员工类别:

public class Employee {

   //instance variable
   private String name;
   private String lastName;
   private double salary;

    //getters
    public String getLastName() {
        return lastName;
    }
    public String getName() {
        return name;
    }
    public double getSalary() {
        return salary;
    }

    public double raise(double raise){
        salary = salary + (salary*.1);
        return salary;
    }

    //constructor
    public Employee(String name, String lastName, double salary){

        this.name = name;
        this.lastName = lastName;
        this.salary = salary;

    }

}


打印以上程序的输出:

员工#1

名:贝蒂

姓氏:Jones

薪酬:$ 4000.00

她的加薪将是:$ 4400.00

员工#2

名:Sally

姓氏:Mae

薪酬:$ 6000.00

她的加薪将是:$ 6600.00

10-08 02:15