IF声明没有打印正确的数字?

时间:2014-04-01 17:58:51

标签: java if-statement int logic

我正在用Java创建一个简单的税收支架应用程序用于家庭作业项目。

它需要总收入,减去用户拥有的豁免数量,并减去总额等于应税收入的标准扣除额,将其纳入税率范围并进行计算以告知用户应缴纳的税款。

我知道在IF报表中设置的应税和税率,应该对应缴税额进行适当的计算,但它几乎完全加倍了价值。

IF语句是:

if (taxable >= 1 && taxable <= 9075){
        taxrate = (int) (taxable * .10);
    } else if (taxable >= 9076 && taxable <= 36900) {
        taxrate = (int) (taxable * .15)  + 908;
    } else if (taxable >= 36901 && taxable <= 89350) {
        taxrate = (int) (taxable * .25)  + 5082;
    } else if (taxable >= 89351 && taxable <= 186350){
        taxrate = (int) (taxable * .28) + 18195;
    } else if (taxable >= 186351 && taxable <= 405100){
        taxrate = (int) (taxable * .33) + 45354;
    } else if (taxable >= 405101 && taxable <= 406750) {
        taxrate = (int) (taxable * .35) + 117541;
    } else if (taxable >= 406751) {
        taxrate = (int) (taxable * .396) +118119;
    }

末尾随机添加的数字是之前的税费到期税,它将其添加到下一个括号,依此类推。我知道这可能是一个非常简单的错误,所以任何Java专家都有解决方案吗?

此外,应税的代码是:

static int taxable = gross - exempt - standard;

总数可以等于任何数字,与免税和标准相同

1 个答案:

答案 0 :(得分:2)

我认为计算给定收入范围(rangeminrangemax)的税收的通用公式应为:

taxrate = (taxable - rangemin) * rangePerc + rangemin * previousRangePerc

这样,在你的代码中:

if (taxable >= 1 && taxable <= 9075){
        taxrate = (int) (taxable * .10);
} else if (taxable >= 9076 && taxable <= 36900) {
        taxrate = (int) ((taxable - 9076) * .15)  + 908;
} else if (taxable >= 36901 && taxable <= 89350) {
    taxrate = (int) ((taxable - 36901) * .25)  + 5082;
} ...