0

所以我真的很困惑我做错了什么,我知道我犯了一个愚蠢的错误,但我真的需要帮助。如果有人可以给我一个提示,将不胜感激。我正在编写一些代码来计算一个人的收入,但由于某种原因,它只是出现了信用。

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());
    }
}
4

3 回答 3

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());
    }
}

我建议不要只从构造函数本身调用该方法。这不是一个很好的做法。

于 2013-06-14T04:32:53.633 回答
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;
于 2013-06-14T04:08:47.447 回答
-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());
    }
}

或者

用这个替换你的构造函数:

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。

于 2013-06-14T03:44:06.110 回答