如何压缩这个java程序?

时间:2014-09-08 17:58:01

标签: java

我正在尝试压缩这段代码,使其更加精简。我知道必须有一种方法可以使用过去的计算if if语句在下一个纳税括号计算中使用它。我试图将tax = ph放在其他if if语句中以便在下一次计算中使用(对于占位符值),但它会忽略它。 编辑:这里重写这个听起来不像反馈。我的代码太长了怎么才能缩短它?此外,我必须在此作业中使用switch语句。

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

   System.out.print(
  "(0-single filer, 1-married jointly or qualifying widow(er), "
  + "\n2-married separately, 3-head of household)\n" +
  "Enter the filing status: ");

int status = input.nextInt();

System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
double tax = 0;



switch (status){
    case 0: {      // file single
       if (income <= 8350)
       tax = income * 0.10;
       else if (income <= 33950)
        tax = 8350 * 0.10 + (income - 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;
           }
    break;
    case 1:{      //file married or Widow  
        if (income <= 16700)
          tax = income * 0.10;
        else if(income <= 67900)
           tax = 16700 * .10 + (income - 16700) * .15;
        else if (income <= 137050)
            tax = 16700 * .10 + (67900 - 16700) * .15 + (income - 137050) * .25;
        else if (income <= 208850)
            tax = 16700 * .10 + (67900 - 16700) * .15 + (137050 - 67900) *  .25 + 
                    (income - 208850) * .28;
        else if (income <= 372950)
            tax = 16700 * .10 + (67900 - 16700) * .15 + (137050 - 67900)  * .25 + 
                    (208850 - 137050) * .28 + (income - 372950) * .33;
        else
            tax =  16700 * .10 + (67900 - 16700) * .15 + (137050 - 67900) * .25 + 
                    (208850 - 137050) * .28 + (372950 - 208850) * .33 + (income - 372950) * .35;
    }
    break;

}
System.out.println("Your tax will be $" + tax);


}

}

3 个答案:

答案 0 :(得分:1)

只是一个建议,但你可以逐步增加“税收”并减少“收入”。

例如:

tax = income * 0.10; // 10% you always pay
income = Math.max(0, income-8350); // Take away the first 8350, on which we already have 10% tas
tax += income * 0.05; // 5% more on income higher than 8350
income = Math.max(0, income-33950); // Take away 33950, on wich we already gave 5% more taxes
tax += income * 0.10; // 10% more on income higher than 33950

等等。

仔细检查我的数学,我没有探索你的所有ifs,但我用这种方式来计算税收。

随意提出澄清。

答案 1 :(得分:0)

你可以做什么(只显示案例0的一部分):

tax = income * 0.10;
income -= 8350;
if (income > 0) {
    tax += income * (0.15 - 0.1);
    income -= 33950 - 8350;
}
if (income > 0) {
    tax += income * (0.25 - 0.15);
    income -= 82250 - 33950;
}
...

这也表明,当你将常量存储在两个数组中时,如何使用循环来进行计算。

答案 2 :(得分:0)

这个版本的最小条件检查是以额外的计算为代价,生成相当简洁的代码,所有“魔术”数字在一个地方很容易看到。

public static void main(String[] args) {
    int income = 1000000;  // Sample test amount of income, should result in 327,683.50 in taxes.
    double tax = 0;
    if (income > 0) {
        tax = income * 0.10;
        tax += (Math.max(0, income-8350)) * 0.05;
        tax += (Math.max(0, income-33950)) * 0.10;
        tax += (Math.max(0, income-82250)) * 0.03;
        tax += (Math.max(0, income-171550)) * 0.05;
        tax += (Math.max(0, income-372950)) * 0.02;
    }
    System.out.println("Tax: "  + tax);
}