2

我刚刚在大学完成了 Java 测试,我知道我回答错了一个特定的问题,希望得到一些帮助/澄清吗?

问题如下:

实施一种获取某人收入并计算税款的方法。如果该人的收入低于 7500,则税金 = 0。如果该人的收入在 7501 和 45000 之间,则税金 = 20%,减去 7500,即免税。最后,如果该人的收入高于 45001,则税 = 40%,减去 20% 范围内的收入,然后减去免税的 7500。

由于时间不多了,我只是做了一个基本的 if else 声明,显示了收入和税级,下面的例子。

public static double incomeTax(double income){

    if(income <= 7500){
           income = income * 0;
       }
     else if(income >= 7501 && income <= 45000){
           income = income * 0.8;
       }
     else(income >= 45001){
           income = income * 0.6;
       }
     return income;
} 

我知道代码不正确,离我们很近,但在测试即将结束时,我试了一下,希望只是为 if else 语句打分。

我真的很感激这里的任何帮助。

谢谢你。

在得到很好的反馈之后,这就是我回来的结果(有很多帮助!!:])...

import java.util.Scanner;

public class TaxableIncome
{
public static void main(String[] args){
    netIncome();
}

public static double netIncome() {
    double income = 0;

    Scanner in = new Scanner(System.in);

    System.out.print("Enter income: ");
    income = in.nextDouble();
    System.out.println();

    double tax1 = 0;
    double tax2 = 0;
    double totalTax = tax1 + tax2;

    // high income bracket
    if (income > 45000) {
        double part1 = income - 45000; // part = income - 45000
        tax1 += part1 * 0.4; // tax = tax + part * 0.4
        System.out.println("High Tax Band - Greater than 45000: " + tax1);
    }

    // medium income bracket
    if (income >  7500) {
        double part2 = income - 7500;
        tax2 += part2 * 0.2;
        System.out.println("Medium Tax Band - Greater than 7500: " + tax2);
    }

    System.out.println("Total Tax = " + (tax1 + tax2));

    // tax for low income is zero, we don't need to compute anything.

    return totalTax;
   }
}
4

6 回答 6

1

一个简单的答案是这样的:

public static double netIncome(double income) {

  double tax = 0;

  // high income bracket
  if (income > 45000) {
    double part = income - 45000;
    tax += part * 0.4;
    income = 45000;
  }

  // medium income bracket
  if (income >  7500) {
    double part = income - 7500;
    tax += part * 0.2;
  }

  // tax for low income is zero, we don't need to compute anything.

  return tax;
}

这样您就可以计算每个税级的税额并将它们相加。

于 2013-02-06T21:31:50.383 回答
0

您需要多次尝试应用税率。您的代码只达到一个税级。如果我赚了 100k,我会为整个 100k 缴纳 40% 的税。这是我很快想到的。

public static double incomeTax(double income)
{
    double tax = 0.0;
    int midLevel = 7500;
    int highLevel = 45000;

    if (income <= 7500)
    {
        // Could early exit at this point and return already set tax
        tax = 0.0;
    }

    if (income > midLevel)
    {
        // Find range of income > 7500, less than 4500. 37499 max
        double taxableIncome = Math.min(income - midLevel, highLevel - midLevel - 1);
        tax += taxableIncome * 0.2;
    }

    if (income > highLevel)
    {
        // Income above 45k
        double taxableIncome = income - highLevel;
        tax += taxableIncome * 0.4;
    }

    return tax;
} 
于 2013-02-06T21:35:42.170 回答
0

我看不出逻辑有什么问题。您遇到的主要问题是您只需要退还所得税,而不是总收入。所以你需要返回的值是收入*whateverPercentTax。

另外,我会尝试过:

if(income < 7001){
}else if(income >=45001){
}else{}

但是,这只是我。

于 2013-02-06T21:29:47.247 回答
0

您必须对乐队征税。脏(未经测试)代码:

public static double incomeTax(double income){
    double tax = 0;
    if(income > 7500){
        tax += Math.min(45000-7500, income-7500)*.2;
    }

    if(income > 45000){
        tax += (income-45000)*.4;
    }
    return tax;
}
于 2013-02-06T21:30:25.647 回答
0

你可以试试这个,只需将你的收入括号复制到括号数组中。确保括号数组中有无穷大,并从速率数组中的0开始。

import java.util.Scanner;
import java.text.DecimalFormat;
class IncomeTaxWithBrackets {
public static void main(String[] args) {

    double infinity = Double.POSITIVE_INFINITY;
    double [] bracket = {0, 7565, 25903, 54987, 121121, 567894, infinity}; 
    double [] rate = {0, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35};
    // bracket[0] to bracket[1] are assigned rate[1]

    double income = 0;
    double tax = 0;

    System.out.print("\nPlease enter your income: ");

    Scanner keyboard = new Scanner (System.in);
    DecimalFormat dollar = new DecimalFormat ("$#,##0.00");

    income = keyboard.nextDouble();

    int x,i;

    for (i=0; i <= 5; i++) {
        if (income > bracket[i] && income <= bracket[i+1]) {
            for (x=0; x<i; ++x) {
                tax = tax + (bracket[x+1] - bracket[x]) * rate[x+1];
            }   
            tax = tax + (income - bracket[i]) * rate[i+1];
        }
    }       
    System.out.println("\nWith a taxable income of "+dollar.format(income)+", your personal income tax is "+dollar.format(tax));
}
  }

让我知道你的想法。

于 2013-11-05T04:30:53.450 回答
0

我会从这样的事情开始:

public static double incomeTax(int income) {
    final double band00Income = (double) Math.min(income, 7500);
    final double band20Income = (double) Math.min(income - band00Income, 45000 - 7500);
    final double band40Income = (double) Math.max(income - 45000, 0);

    final double tax = band20Income * 0.2 + band40Income * 0.4;

    return tax;
}

请注意,由于英国税收计算的特殊性,收入是一个整数 - 它还解决了 7500.01 和 7500.99 (含)之间未指定情况的问题。

更好的解决方案是提取所有幻数的常量。一个更好的解决方案是将频段和速率概括到一个表格中,以便可以轻松更改它们。

一个完整的答案可能包括这样的测试用例:

import org.junit.Assert;
import org.junit.Test;

public class TestTax
{
    public static final double DELTA = 0.1;

    @Test
    public void testTax() {
        Assert.assertEquals(0.0, incomeTax(-3000), DELTA);
        Assert.assertEquals(0.0, incomeTax(0), DELTA);
        Assert.assertEquals(0.2, incomeTax(7501), DELTA);
        Assert.assertEquals(3000.0, incomeTax(22500), DELTA);
        Assert.assertEquals(7500.0, incomeTax(45000), DELTA);
        Assert.assertEquals(7500.4, incomeTax(45001), DELTA);
        Assert.assertEquals(25500.0, incomeTax(90000), DELTA);
    }

    public static double incomeTax(int income) {
        final double band00Income = (double) Math.min(income, 7500);
        final double band20Income = (double) Math.min(income - band00Income, 45000 - 7500);
        final double band40Income = (double) Math.max(income - 45000, 0);

        final double tax = band20Income * 0.2 + band40Income * 0.4;

        return tax;
    }
}
于 2013-02-06T21:44:37.807 回答