Tax Bracket处理if和else if

时间:2013-09-26 12:47:44

标签: java

我在尝试获取正确的值以解决此程序时遇到问题。该计划的目的是计算总收入税,我认为这可能是一个舍入错误,但我不确定。我运行了多个测试用例,数字越大,错误越大。

测试案例: 单身   一个。税:10,240 湾正确输出1118.5 实际产量1118

2。 共同填补 征税70,000 正确输出9875 实际产量9665

import java.util.Scanner;

public class Tax
{

public static void main(String args[])
{
    Scanner scan=new Scanner(System.in);
    int repeats=0;
    System.out.println("How many people are you filing for?");
    repeats=scan.nextInt();
    for(int i=0;i<repeats;i++)
        calculate();
}
public static void calculate()
{
    Scanner scan=new Scanner(System.in);
    boolean isGood=false;
    int bracket=0;
    while(isGood==false)
    {
    System.out.println("Please choose your Filing Status:\n1: Single\n2: Married Filed Jointly\n3: Married Filling Separately\n4: Head of Household");
    bracket=scan.nextInt();
    if(bracket>0&&bracket<5)
        isGood=true;
    }
    System.out.println("Please enter your income (Round to the nearest dollar)");
    double income=scan.nextDouble();
    double [] sBracket={8350,33950,82250,171550,372950};
    double [] fjBracket={16700,67900,137050,208850,372950};
    double [] fsBracket ={8350,33950,68525,104425,186475};
    double [] hhBracket={11950,45500,117450,190200,372950};
    double [] myBracket;
    double [] percents={.1,.15,.25,.28,.33,.35};
    myBracket=sBracket;
    if(bracket==2)
        myBracket=fjBracket;
    if(bracket==3)
        myBracket=fsBracket;
    if(bracket==4)
        myBracket=hhBracket;
    int sum=0;





    if(income<myBracket[0])
        {
        sum+=income*percents[0];
        }       
    else
        {
            sum+=(myBracket[0]*percents[0]);
            income-=myBracket[0];


            if(income>myBracket[1])
                {
                    sum+=(myBracket[1]*percents[1]);
                    income-=myBracket[1];
                    System.out.println("If Ran");
                }
            else if(income<myBracket[1]&&income>0)
                {
                    System.out.println("Else IF ran");
                    sum+=(income*percents[1]);
                    income-=myBracket[1];
                }

            if(income>myBracket[2])
                {
                    sum+=(myBracket[2]*percents[2]);
                    income-=myBracket[2];
                }
            else if(income<myBracket[2]&&income>0)
                {
                    sum+=(income*percents[2]);
                    income-=myBracket[2];
                }

            if(income>myBracket[3])
                {
                    sum+=(myBracket[3]*percents[3]);
                    income-=myBracket[3];
                }
            else if(income<myBracket[3]&&income>0)
                {
                    sum+=(income*percents[3]);
                    income-=myBracket[3];
                }
            if(income>myBracket[4])
                {
                    sum+=(income*percents[5]);
                    income-=myBracket[4];
                }

        }
    System.out.println("Your Tax is :"+sum);    
}
}

2 个答案:

答案 0 :(得分:1)

你使用了sum作为int。请使用sum作为double,看看魔法。它将值总和为四舍五入,因此您在结果中得到错误的值。

import java.util.Scanner;

public class Tax
{

    public static void main(String args[])
    {
        Scanner scan=new Scanner(System.in);
        int repeats=0;
        System.out.println("How many people are you filing for?");
        repeats=scan.nextInt();
        for(int i=0;i<repeats;i++)
            calculate();
    }
    public static void calculate()
    {
        Scanner scan=new Scanner(System.in);
        boolean isGood=false;
        int bracket=0;
        while(isGood==false)
        {
        System.out.println("Please choose your Filing Status:\n1: Single\n2: Married Filed Jointly\n3: Married Filling Separately\n4: Head of Household");
        bracket=scan.nextInt();
        if(bracket>0&&bracket<5)
            isGood=true;
        }
        System.out.println("Please enter your income (Round to the nearest dollar)");
        double income=scan.nextDouble();
        double [] sBracket={8350,33950,82250,171550,372950};
        double [] fjBracket={16700,67900,137050,208850,372950};
        double [] fsBracket ={8350,33950,68525,104425,186475};
        double [] hhBracket={11950,45500,117450,190200,372950};
        double [] myBracket;
        double [] percents={.1,.15,.25,.28,.33,.35};
        myBracket=sBracket;
        if(bracket==2)
            myBracket=fjBracket;
        if(bracket==3)
            myBracket=fsBracket;
        if(bracket==4)
            myBracket=hhBracket;
        double sum=0;





        if(income<myBracket[0])
            {
            sum+=income*percents[0];
            }       
        else
            {
                sum+=(myBracket[0]*percents[0]);
                income-=myBracket[0];


                if(income>myBracket[1])
                    {
                        sum+=(myBracket[1]*percents[1]);
                        income-=myBracket[1];
                        System.out.println("If Ran");
                    }
                else if(income<myBracket[1]&&income>0)
                    {
                        System.out.println("Else IF ran");
                        sum+=(income*percents[1]);
                        income-=myBracket[1];
                    }

                if(income>myBracket[2])
                    {
                        sum+=(myBracket[2]*percents[2]);
                        income-=myBracket[2];
                    }
                else if(income<myBracket[2]&&income>0)
                    {
                        sum+=(income*percents[2]);
                        income-=myBracket[2];
                    }

                if(income>myBracket[3])
                    {
                        sum+=(myBracket[3]*percents[3]);
                        income-=myBracket[3];
                    }
                else if(income<myBracket[3]&&income>0)
                    {
                        sum+=(income*percents[3]);
                        income-=myBracket[3];
                    }
                if(income>myBracket[4])
                    {
                        sum+=(income*percents[5]);
                        income-=myBracket[4];
                    }

            }
        System.out.println("Your Tax is :"+sum);    
    }
}

答案 1 :(得分:1)

如何将sum变量从int更改为double

我之所以这么说是因为你会得到最终总和的小数精度,尤其是你说的话:

正确输出1118.5实际输出1118

实际输出则为1118.5

double sum = 0.0; // rest of code
System.out.println("Your tax is: " + sum);