0

我在尝试获取正确的值以在该程序中显示时遇到问题。该计划的目的是计算总所得税我认为这可能是一个四舍五入的错误,但我不确定。我已经运行了多个测试用例,并且数字越大,错误越大。

测试用例: 1. 单一个。税:10,240 b。正确输出 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);    
}
}
4

2 回答 2

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);    
    }
}
于 2013-09-26T12:50:22.347 回答
1

int将 sum 变量从 a 更改为 a怎么样double

我之所以这么说是因为您将在最终总和中获得小数精度,尤其是在您说:

正确输出 1118.5 实际输出 1118

实际输出将是 1118.5

double sum = 0.0; // rest of code
System.out.println("Your tax is: " + sum);
于 2013-09-26T12:50:29.347 回答