首页 > 解决方案 > 转让税计算器

问题描述

我有一个任务要写一个税收计算器,我已经完成了,但基本上是从我的教科书上抄来的。我只是想要一个关于如何重写这个的提示......当我在网上做这个时,任何帮助都是值得的,这是我的第一堂课,我觉得很难。

我试图将计算放入表格中,但并不真正了解如何使其工作......

import java.util.Scanner;

public class TaxCalculater {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    // Enter Filing status
    System.out.println( "0 = Single\n1 = Married Filing Jointly or Qualified Widow(er)\n" +
                    "2 = Married Filing Separately\n3 = Head of Household" );
    int status = input.nextInt();
    // Enter Income
    System.out.print("Please enter the taxable income: ");
    double income = input.nextDouble();

    //Compute taxable
    double tax = 0;

    if (status == 0){
        if (income <= 8350)
            tax = income * 0.10;
        else if (income <= 33950)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15;
        else if (income <= 82250)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (income - 33950) * 0.25;
        else if (income <= 171550)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
        else if (income <= 372950)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
                (income - 171550) * 0.33;
        else
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
                (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
    }
        else if (status == 1){
        if (income <= 16700)
            tax = income * 0.10;
        else if (income <= 67900)
            tax = 16700 * 0.10 + (67900 - 16700) * 0.15;
        else if (income <= 137050)
            tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
                (income - 67900) * 0.25;
        else if (income <= 208850)
            tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
                (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
        else if (income <= 372950)
            tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
                (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
                (income - 208850) * 0.33;
        else
            tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
                (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
                (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
    }
        else if (status == 2){
        if (income <= 8350)
            tax = income * 0.10;
        else if (income <= 33950)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15;
        else if (income <= 68525)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (income - 33950) * 0.25;
        else if (income <= 104425)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
        else if (income <= 186475)
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
                (income - 104425) * 0.33;
        else
            tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
                (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
                (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
    }
        else if (status == 3){
        if (income <= 11950)
            tax = income * 0.10;
        else if (income <= 45500)
            tax = 11950 * 0.10 + (45500 - 11950) * 0.15;
        else if (income <= 117450)
            tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
                (income - 45500) * 0.25;
        else if (income <= 190200)
            tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
                (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
        else if (income <= 372950)
            tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
                (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
                (income - 190200) * 0.33;
        else
            tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
                (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
                (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
    }
    else {
        System.out.println("Error: invalid status");
        System.exit(1);
    }

    // Display result
    System.out.println("Tax is " + (int)(tax * 100) / 100.0);
  }
}

上面的代码正在运行,只是想要一些关于如何以不同方式编写它的想法/提示。谢谢!!

标签: java

解决方案


通常,您不希望 main 方法中有很多东西,而是希望它尽可能干净。考虑到这一点,看看您是否可以为不同的税级创建一些通用方法,也许在另一个类中,然后从 main 内部调用它们。也许您还可以找到一种方法来使用 switch case 而不是所有那些 if。


推荐阅读