gpt4 book ai didi

java - 嵌套 if 语句遇到问题

转载 作者:行者123 更新时间:2023-12-01 08:12:49 26 4
gpt4 key购买 nike

理论认为,不同收入层次的税率不同。前 5 万个 10%,接下来的 5 万个 15%,超过 10 万个 25%。

 public double getTaxesWithheld() {

if (taxableIncome >= 100000.0) {

taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
taxableIncome = taxableIncome - 100000.0;
} else {
if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
taxableIncome = taxableIncome - 50000.0;
} else {
if (taxableIncome < 50000.0) {
taxesWitheld = taxesWitheld + (.1 * (taxableIncome - 25000.0));
}
}

if (taxableIncome <= 0) {
return 0.0;
}

}
return taxesWitheld;
}

测试用例1: @测试构造函数值(first_name、last_name、job_title、id、month_salary)

public void EmployeeMakingBetween50Kand100K() {
Employee h = new EmployeeImpl("Jon", "Smith", "Miner", 2222, 6166.75);


assertEquals(h.getMonthlySalary(), 6166.75, 0.005);
assertEquals(h.getGrossYearlyIncome(), 6166.75*12, 0.005);
assertEquals(h.getTaxableIncome(), h.getGrossYearlyIncome(), 0.005);
assertEquals(h.getTaxesWithheld(), 8600.15, 0.005);
assertEquals(h.getNetYearlyIncome(), h.getGrossYearlyIncome()-h.getTaxesWithheld(), 0.005);
}

不会处理taxWithheld(), 8600.15, 0.005

最佳答案

这是一种完全不同的方法,可能会产生更好的结果:

 public double getTaxesWithheld() {

over100k = Math.max(taxableIncome-100000, 0);
taxableIncome -= over100k;
over50k = Math.max(taxableIncome-50000, 0);
taxableIncome -= over50k;

taxesWitheld = taxesWitheld + (.25 * over100k);
taxesWitheld = taxesWitheld + (.15 * over50k);
taxesWitheld = taxesWitheld + (.1 * taxableIncome);
return taxesWitheld;
}

关于java - 嵌套 if 语句遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992005/

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