基本的 Java 所得税

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29322416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:03:39  来源:igfitidea点击:

Basic Java Income Tax

java

提问by Michael

The Question:

问题:

Write a program that prompts the user to enter his/her taxable income and then calculates the income tax due using the 2014 tax table below. Express the tax with two decimal places.

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

Income tax brackets for single-filers
up to 75          10%
76 - 900    15%
901 - 350       25%
351 - 6350      28%
6351 - 5100   33%

This is what I have so far. I'm really new to Java so keep that in mind. My specific question will come after my code.

这就是我迄今为止所拥有的。我对 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.");
    }
}

The final output should look like this:

最终输出应如下所示:

Enter the amount of taxable income for the year 2014. (user input here ->) 5000

输入2014年的应纳税所得额。(用户在此输入->)5000

You have entered the 10% tax bracket.

您已输入 10% 的税级。

Your income is $5,000.00, Your tax is: $500.00. Your income after tax is: $4,500.00

您的收入为 5,000.00 美元,您的税为:500.00 美元。您的税后收入为:$4,500.00



How do I get my output to look like the above output? Specifically the decimals and the $ sign.

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

The output code that I am currently working on is:

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

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

回答by Willem Van Onsem

Simply append the following part to the code:

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

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));

Here the DecimalFormatteris a technique to format numbers properly (for instance with commas separating the thousands, and two digits after the decimal dot).

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

But your code is quite sloppy and error-prone and sometimes doesn't seem to make much sense. You can better subdivide the calculation of taxes using different brackets:

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

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;
}

回答by KSFT

All you need to do is print the tax. You can format it with NumberFormat, as mentioned here:

您需要做的就是打印税款。你可以用格式化NumberFormat,提到这里

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)+".");

回答by Abdelrahman Elkady

simplest way to use DecimalFormat

使用DecimalFormat 的最简单方法

you will need to import java.text.DecimalFormat

你需要导入 java.text.DecimalFormat

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

will output :

将输出:

1,122,334,455.00

1,122,334,455.00