gpt4 book ai didi

java - 第一次使用JAVA中的方法和构造函数,在程序中收到错误以确定引发

转载 作者:行者123 更新时间:2023-12-02 06:13:20 24 4
gpt4 key购买 nike

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

这是我的代码:

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 方法不会返回任何内容,这就是 raise 方法没有按照您想要的方式工作的原因。我已为您修复了该问题,请看一下,希望我能帮助您了解哪里出了问题,如果您有任何问题,请随时询问,我随时为您服务。

员工测试类:

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

名字:贝蒂

姓氏:琼斯

薪资:$4000.00

她的加薪金额为:4400.00 美元

员工#2

名字:莎莉

姓氏:梅

薪资:$6000.00

她的加薪将是:$6600.00

关于java - 第一次使用JAVA中的方法和构造函数,在程序中收到错误以确定引发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692282/

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