所以我真的很困惑我做错了什么,我知道我犯了一个错误的错误,但我真的需要帮助。如果有人能给我一个提示,我将不胜感激。 我正在研究一些可以计算单个人收入的代码,但由于某种原因,它只是出现的功劳。
public class TaxReturn
{
private double income;
private double deductions;
private int numberofExemptions;
private double credits;
private double tax;
public TaxReturn(double salary, double deductable, int exempt,
double creditable)
{
tax = 0;
income = salary;
deductions = deductable;
numberofExemptions = exempt;
credits = creditable;
}
public void calculateTax()
{
final double exempt = 3800;
double exemption = numberofExemptions * exempt;
double taxableIncome = income - deductions - exemption;
final double rate_10 = 0.1;
final double rate_15 = 0.15;
final double rate_25 = 0.25;
final double rate_28 = 0.28;
final double rate_33 = 0.33;
final double rate_35 = 0.35;
final double rate_395 = 0.395;
final double income1 = 8925;
final double income2 = 36250;
final double income3 = 87850;
final double income4 = 183250;
final double income5 = 398350;
final double income6 = 400000;
while(taxableIncome != 0)
{
if(taxableIncome > income6)
{
tax = (taxableIncome - income6) * rate_395;
}
else if(taxableIncome > income5)
{
tax = (taxableIncome - income5) * rate_35;
}
else if(taxableIncome > income4)
{
tax = (taxableIncome - income4) * rate_33;
}
else if(taxableIncome > income3)
{
tax = (taxableIncome - income3) * rate_28;
}
else if(taxableIncome > income2)
{
tax = (taxableIncome - income2) * rate_25;
}
else if(taxableIncome > income1)
{
tax = (taxableIncome - income1) * rate_15;
}
else
{
tax = (taxableIncome) * rate_10;
}
}
}
public int printTaxReturn()
{
int amount = (int) ((int)Math.ceil(tax) - credits);
return amount;
}
}
这是我用来测试代码的方法
public class TaxReturnTester
{
public static void main(String[] args)
{
TaxReturn tax = new TaxReturn(80500.5, 20000.00, 3, 5000.00);
System.out.println(tax.printTaxReturn());
}
}
答案 0 :(得分:1)
你的问题主要是你没有调用在任何地方进行计算的方法。
public class TaxReturnTester
{
public static void main(String[] args)
{
TaxReturn tax = new TaxReturn(80500.5, 20000.00, 3, 5000.00);
tax.calculateTax()
System.out.println(tax.printTaxReturn());
}
}
我建议不要只从构造函数本身调用该方法。这不是一个很好的做法。
答案 1 :(得分:0)
您可以使用多个while
。
if
循环
if(taxableIncome > income6)
{
tax = (taxableIncome - income6) * rate_395; taxableIncome -= income6;
}
if(taxableIncome > income5)
{
tax = (taxableIncome - income5) * rate_35; taxableIncome -= income5;
}
if(taxableIncome > income4)
{
tax = (taxableIncome - income4) * rate_33; taxableIncome -= income4;
}
if(taxableIncome > income3)
{
tax = (taxableIncome - income3) * rate_28; taxableIncome -= income3;
}
if(taxableIncome > income2)
{
tax = (taxableIncome - income2) * rate_25; taxableIncome -= income2
}
if(taxableIncome > income1)
{
tax = (taxableIncome - income1) * rate_15; taxableIncome -= income1;
}
tax = (taxableIncome) * rate_10;
答案 2 :(得分:-2)
你必须调用calculateTax()。
您有两种选择:
将TaxReturnTester替换为:
public class TaxReturnTester
{
public static void main(String[] args)
{
TaxReturn tax = new TaxReturn(80500.5, 20000.00, 3, 5000.00);
tax.calculateTax();
System.out.println(tax.printTaxReturn());
}
}
OR
用这个替换你的构造函数:
public TaxReturn(double salary, double deductable, int exempt,
double creditable)
{
tax = 0;
income = salary;
deductions = deductable;
numberofExemptions = exempt;
credits = creditable;
calculateTax();
}
我的第二个事实是你应该使用BigDecimal代替花车来计算金钱。如果没有,你会看到不一致。
Thihara建议使用选项1.如果您不希望每次创建对象时都运行calculateTax(),那么您肯定希望使用选项1.