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