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

you have used sum as int. please use sum as double and see the magic. It is rounding the values in sum so you are getting wrong value in result.

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

What about changing your sum variable from an int to a double.

The reason why I say that is because you will get the decimal precision in the final sum, especially where you say:

Correct Output 1118.5 Actual output 1118

the actual output would then be 1118.5

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