gpt4 book ai didi

java - 为每个循环设置值

转载 作者:行者123 更新时间:2023-11-30 07:32:11 24 4
gpt4 key购买 nike

遇到问题了。我有两种带有 foreach 循环的方法,可以按百分比或基本工资计算高管薪酬。在我的行政级别中,我的薪酬方法采用基本工资率并乘以奖金。如果它是百分比,但如果它的基本工资和调用此方法不起作用,则此方法有效。我是否在我的高管级别中添加了 if 语句来查看它是百分比还是基本工资?

员工类(class)

/**
* Assigns the specified flat value weekly bonus to the Executives.
*
* @param bonusValue
* as a double, i.e., $1,000 = 1000.0
*/

public void setExecutiveBonusFlatRate(double bonusValue) {
for (StaffMember executiveEmployee : staffList) {
if (executiveEmployee instanceof Executive) {
((Executive) executiveEmployee).setBonus(bonusValue);

}
}
}



/**
* Assigns the specified percentage weekly bonus to the Executives.
*
* @param bonus
* as a percentage, i.e., 20% = 0.2
*/

public void setExecutiveBonusPercentage(double bonusPercentage) {
for (StaffMember executiveEmployee : staffList) {
if (executiveEmployee instanceof Executive) {
((Executive) executiveEmployee).setBonus(bonusPercentage);

}
}
}

/**
* Pays all the staff members.
*/

public void payday() {
for (StaffMember allEmployee : staffList) {
allEmployee.toString();
System.out.println(allEmployee.pay());
System.out.println(allEmployee.toString());
}
}

从Executive类扩展自Employee

/**    @overide
* return the weekly payrate plus the bonus
*/

public double pay() {

double payment = payRate * bonus;
bonus = 0;
return payment;

最佳答案

我们需要在这里纠正两件事:

  1. setExecutiveBonusPercentage 应将奖金设置为 base * Percentage * 0.01,以与 setExecutiveBonusFlatRate< 中设置的 bonusValue 一致 因为我们无法知道 bonus 是一个值还是百分比。

  2. pay() 方法中,我们将奖金设置为 0 (bonus = 0;),在重置奖金值时需要将其删除。因此,第一次调用 pay() 将返回正确的结果,而后续调用将返回 0。

关于java - 为每个循环设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35952454/

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