Java 使用特定值计算税收
我无法理解如何让我的程序正确计算这些税号Java 使用特定值计算税收,java,Java,我无法理解如何让我的程序正确计算这些税号 如果单项应纳税所得额超过 但还没有结束 税是 超过的金额 $0 $8,000 10% $0 $8,000 $32,000 $800 + 15% $8,000 $32,000 $4,400 + 25% $32,000 If married and taxable income is over but not over the tax is of the amount over $0 $16,000 10% $0 $16,000
如果单项应纳税所得额超过 但还没有结束 税是 超过的金额
$0
$8,000 10% $0
$8,000 $32,000 $800 + 15% $8,000
$32,000
$4,400 + 25% $32,000
If married and taxable income is over but not over the tax is of the amount over
$0 $16,000 10% $0
$16,000 $64,000 $1,600 + 15% $16,000
$64,000
$8,800 + 25% $64,000
You should prompt the user for their filing status and their taxable income. The user should enter 'S' for single filers and 'M' for married filers.
例如,如果纳税人是单身,收入为10000美元。他们将支付800美元加上2000年的15%(10000-8000)。
这就是我目前所拥有的
public class Income_tax
{
public static final int SINGLE = 1;
public static final int MARRIED = 2;
private static final double RATE1 = 0.10;
private static final double RATE1_SINGLE_LIMIT = 8000;
private static final double RATE1_MARRIED_LIMIT = 16000;
private static final double RATE2_SINGLE_LIMIT = 32000;
private static final double RATE2_MARRIED_LIMIT = 64000;
private static final double RATE3_SINGLE_LIMIT = 60000;
private static final double RATE3_MARRIED_LIMIT = 150000;
private static final double RATE2 = 0.15;
private static final double RATE3 = 0.25;
private double income;
private int status;
public Income_tax(double anIncome, int aStatus)
{
income = anIncome;
status = aStatus;
}
public double getTax()
{
double tax1 = 0;
double tax2 = 0;
if (status == SINGLE)
{
if (income <= RATE1_SINGLE_LIMIT)
{
tax1 = RATE1 * income;
}
else
{
tax1 = RATE1 * RATE1_SINGLE_LIMIT;
tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT);
}
}
if (income <= RATE3_SINGLE_LIMIT)
{
tax1 = RATE3 * income;
if (income <= RATE1_MARRIED_LIMIT)
{
tax1 = RATE1 * income;
}
if (income <= RATE3_MARRIED_LIMIT)
{
tax1 = RATE3 * income;
}
else
{
tax1 = RATE1 * RATE1_MARRIED_LIMIT;
tax2 = RATE2 * (income - RATE1_MARRIED_LIMIT);
}
if (status == SINGLE)
{
if (income <= RATE2_SINGLE_LIMIT)
{
tax1 = RATE2 * income;
}
else
{
tax1 = RATE1 * RATE2_SINGLE_LIMIT;
tax2 = RATE1 * (income - RATE2_SINGLE_LIMIT);
}
}
else
{
if (income <= RATE2_MARRIED_LIMIT)
{
tax1 = RATE2 * income;
}
else
{
tax1 = RATE2 * RATE2_MARRIED_LIMIT;
tax2 = RATE1 * (income - RATE2_MARRIED_LIMIT);
}
}
return tax1 + tax2;
}
return tax2 + tax1;
}
}
我不太清楚为什么要在main方法中将“aTaxReturn”作为对象调用,这似乎有点不必要。您可以将其作为常规方法调用 有一种方法我会这样做,就是有两种不同的方法,一种是单身,一种是已婚。 然后,在每个单独的方法中,您可以细分税收计算,例如:
public class Income_tax {
private static final double RATE1 = 0.10;
private static final double RATE1_SINGLE_LIMIT = 8000;
private static final double RATE1_MARRIED_LIMIT = 16000;
private static final double RATE2_SINGLE_LIMIT = 32000;
private static final double RATE2_MARRIED_LIMIT = 64000;
private static final double RATE3_SINGLE_LIMIT = 60000;
private static final double RATE3_MARRIED_LIMIT = 150000;
private static final double RATE2 = 0.15;
private static final double RATE3 = 0.25;
//private double income; Don't need these since you're not making an object
//private int status; Don't need these since you're not making an object
//Default constructor since you don't need to make an object
public Income_tax() {
}
public double getTaxSingle(double income) {
//Different if statements for tax calculations
}
public double getTaxMarried(double income) {
//Different if statements for tax calculations
}
}
然后在main方法中,您可以这样调用它:
public static double getTaxRateSingle(double income) {
return Income_tax.getTaxSingle(income);
}
public static double getTaxRateMarried(double income) {
return Income_tax.getTaxMarried(income);
}
这样,您就不需要创建不必要的对象,并且可以在保持简单性的同时最小化代码
还有一个提示:您应该复习一下我不太清楚为什么要在main方法中将“aTaxReturn”作为对象调用,这似乎有点不必要。您可以将其作为常规方法调用 有一种方法我会这样做,就是有两种不同的方法,一种是单身,一种是已婚。 然后,在每个单独的方法中,您可以细分税收计算,例如:
public class Income_tax {
private static final double RATE1 = 0.10;
private static final double RATE1_SINGLE_LIMIT = 8000;
private static final double RATE1_MARRIED_LIMIT = 16000;
private static final double RATE2_SINGLE_LIMIT = 32000;
private static final double RATE2_MARRIED_LIMIT = 64000;
private static final double RATE3_SINGLE_LIMIT = 60000;
private static final double RATE3_MARRIED_LIMIT = 150000;
private static final double RATE2 = 0.15;
private static final double RATE3 = 0.25;
//private double income; Don't need these since you're not making an object
//private int status; Don't need these since you're not making an object
//Default constructor since you don't need to make an object
public Income_tax() {
}
public double getTaxSingle(double income) {
//Different if statements for tax calculations
}
public double getTaxMarried(double income) {
//Different if statements for tax calculations
}
}
然后在main方法中,您可以这样调用它:
public static double getTaxRateSingle(double income) {
return Income_tax.getTaxSingle(income);
}
public static double getTaxRateMarried(double income) {
return Income_tax.getTaxMarried(income);
}
这样,您就不需要创建不必要的对象,并且可以在保持简单性的同时最小化代码
还有一个小贴士:你应该温习一下人们只能纳税一次,所以如果。。。否则如果。。。else情况(根据需要,中间有尽可能多的
else if
)。似乎表格驱动的解决方案更易于管理-非常类似于实际的税务表。人们只能纳税一次,因此,如果。。。否则如果。。。else情况(如果需要,中间有尽可能多的else)。似乎表驱动的解决方案更易于管理-非常类似于实际的税务表。