我有这种方法可以计算申请状态为单身的人的应缴税款。但是我在尝试将其转换为 for 循环或使用 Array 而不是 while 循环时遇到了麻烦,因为这段代码变得非常长而且看起来很讨厌。我想通过简化代码让它看起来很漂亮。有什么建议么?
public void calculateTax()
{
//The constant fields for the tax rate
final double TAXRATE_10 = 0.1; //10%
//This is the tax rate percent on the tax 15%
final double TAXRATE_15PERCENT = 0.15;
//This is the tax rate percent on the tax 25%
final double TAXRATE_25PERCENT = 0.25;
//This is the tax rate percent on the tax 28%
final double TAXRATE_28PERCENT = 0.28;
//This is the tax rate percent on the tax 33%
final double TAXTRATE_33PERCENT = 0.33;
//This is the tax rate percent on the tax 35%
final double TAXRATE_35PERCENT = 0.35;
//constant numbers for tax boundaries.
final int NOTRICH = 8700;
final int MIDDLECLASS = 35350;
final int SORTOFRICH = 85650;
final int RICH = 178650;
final int FORSURERICH = 388350;
//Variables for taxable income, and tax calculation.
long taxableIncome = income - deduction -(numberOfExemption * VAlUEOFEXEMPTION);
double cumulatedTax = 0;
//Calculate the Tax
while(taxableIncome != 0)
{
if(taxableIncome > FORSURERICH)
{
cumulatedTax += ((taxableIncome-FORSURERICH) * TAXRATE_35PERCENT);
taxableIncome = (long)FORSURERICH;
}
else if(taxableIncome > RICH)
{
cumulatedTax += ((taxableIncome-RICH) * TAXTRATE_33PERCENT);
taxableIncome = (long)RICH;
}
else if(taxableIncome > SORTOFRICH)
{
cumulatedTax += ((taxableIncome-SORTOFRICH) * TAXRATE_28PERCENT);
taxableIncome = (long)SORTOFRICH;
}
else if(taxableIncome > MIDDLECLASS)
{
cumulatedTax += ((taxableIncome-MIDDLECLASS) * TAXRATE_25PERCENT);
taxableIncome = (long)MIDDLECLASS;
}
else if(taxableIncome > NOTRICH)
{
cumulatedTax += ((taxableIncome-NOTRICH) * TAXRATE_15PERCENT);
taxableIncome = (long)NOTRICH;
}
else
{
cumulatedTax += ((taxableIncome) * TAXRATE_10);
taxableIncome = 0;
}
}