基本Java所得税

时间:2015-03-28 20:46:22

标签: java

问题:

编写一个程序,提示用户输入他/她的应税收入,然后使用下面的2014年税表计算到期所得税。用小数点后两位表示税。

Income tax brackets for single-filers
up to $9075          10%
$9076 - $36900    15%
$36901 - $89350       25%
$89351 - $186350      28%
$186351 - $405100   33%

这是我到目前为止所拥有的。我是Java新手,所以记住这一点。我的具体问题将在我的代码之后。

import java.util.Scanner;
public class IncomeTax {    


public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Prompt the user to enter taxable income
        System.out.print("Enter the amount of taxable income for the year 2014: ");
        double income = input.nextDouble();

        // Compute tax
        double tax = 0;

        if (income <= 9075)
            tax = income * 0.10;
        else if (income <= 9076)
            tax = 9075 * 0.10 + (income - 36900) * 0.15;
        else if (income <= 36901)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (income - 89350) * 0.25;
        else if (income <= 89351)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (income - 186350) + 0.28;
        else if (income <= 186351)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (89351 - 186350) + 0.28 + (income - 405100) + 0.33;

        if (income <=  9075)
            System.out.println("You have entered the 10% bracket.");
        else if (income <= 9076)
            System.out.println("You have entered the 15% bracket.");
        else if (income <= 36901)
            System.out.println("You have entered the 25% bracket.");
        else if (income <= 89351)
            System.out.println("You have entered the 28% bracket.");
        else if (income <= 186351)
            System.out.println("You have entered the 33% bracket.");
    }
}

最终输出应如下所示:

输入2014年的应纳税所得额。(此处的用户输入 - &gt;)5000

您已输入10%的税率。

您的收入为$ 5,000.00,您的税金为:$ 500.00。您的税后收入是:$ 4,500.00


如何让输出看起来像上面的输出?特别是小数和$符号。

我目前正在处理的输出代码是:

System.out.println("Your income is: " + "Your taxx is: " + (int)(tax * 100) / 100.0) + "Your income after tax is: " + ;

3 个答案:

答案 0 :(得分:4)

只需将以下部分附加到代码中:

DecimalFormat formatter = new DecimalFormat("###,###,###.00");
System.out.println("Your inccome is $"+formatter.format(income)+", your tax is: $"+formatter.format(tax)+". Your income after tax is: $"+formatter.format(income-tax));

这里DecimalFormatter是一种正确格式化数字的技术(例如用逗号分隔千位和小数点后的两位数字)。

但是你的代码非常草率且容易出错,有时似乎没有多大意义。您可以使用不同的括号更好地细分税收计算:

double[] max = {0,9075,36900,89350,186350,405100};
double[] rate = {0,0.10,0.15,0.25,0.28,0.33};
double left = income;
double tax = 0.0d;
for(int i = 1; i < max.length && left > 0; i++) {
    double df = Math.min(max[i]-max[i-1],left);
    tax += rate[i]*df;
    left -= df;
}

答案 1 :(得分:2)

您需要做的就是打印税。您可以使用NumberFormat格式化here

NumberFormat format = NumberFormat.getCurrencyInstance();

System.out.println("You have entered the 10% bracket.");
System.out.println("Your income is $"+format.format(income)+". Your tax is $"+tax+". Your income after tax is "+format.format(income-tax)+".");

答案 2 :(得分:1)

使用DecimalFormat

的最简单方法

您需要导入java.text.DecimalFormat

DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(1122334455));

将输出:

  

<强> 1,122,334,455.00