根据婚姻状况和收入查找所得税 - java

时间:2014-10-23 06:59:25

标签: java

所以我的程序根据你的婚姻状况和收入计算所得税,它是一个班级作业,老师希望我们以某种方式做到这一点,这就是为什么我有最终和其他事情定义的常数。无论如何,我编译了我的程序并且没有语法错误,但是当我运行程序时,计算结束了。

例如,当我将我的婚姻状况输入为单身,然后我的收入为10000时,它给我1000的值,当它应该是1100.

感谢任何帮助!

import java.util.Scanner;

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

  Scanner keyboard = new Scanner(System.in);
  double tax = 0, taxIncome = 0;
  final double FIRST_SINGLE_TAX = .10;
  final double FIRST_MARRIED_TAX = .10;
  final double SECOND_SINGLE_TAX = .15;
  final double SECOND_SINGLE_VALUE = 800;
  final double SECOND_MARRIED_TAX = .15;
  final double SECOND_MARRIED_VALUE = 1600;
  final double THIRD_SINGLE_TAX = .25;
  final double THIRD_SINGLE_VALUE = 4400;
  final double THIRD_MARRIED_TAX = .25;
  final double THIRD_MARRIED_VALUE = 8800;

  System.out.print("Enter your marital status (single or married): ");
  String status = keyboard.nextLine();

  if(!status.equalsIgnoreCase("single") && !status.equalsIgnoreCase("married")) 
  {
     System.out.println("-- illegal marital status --");
     System.exit(0);
  }
  else 
  {
     System.out.print("Enter your taxable income: "); 
     taxIncome = keyboard.nextDouble();
  }

  if (status.equals("single"))

        if (taxIncome >= 0  && taxIncome <= 8000)
        {
           tax = taxIncome * FIRST_SINGLE_TAX;
        }
        else if (taxIncome > 8000 && taxIncome <= 32000)
        {
           tax = (SECOND_SINGLE_VALUE + (taxIncome - 8000) * SECOND_SINGLE_TAX);
        }
        else if (taxIncome >= 32000)
        {
           tax = (THIRD_SINGLE_VALUE + (taxIncome - 32000) * THIRD_SINGLE_TAX);
        }
        else 
        {
        System.out.print(" -- illegal income --");
        }

  else if(status.equals("married"));

       if (taxIncome >= 0 && taxIncome <= 16000)
       {
          tax = taxIncome * FIRST_MARRIED_TAX;
       }
       else if (taxIncome > 16000 && taxIncome <= 64000)
       {
          tax = (SECOND_MARRIED_VALUE + (taxIncome - 16000) * SECOND_MARRIED_TAX);
       }
       else if (taxIncome >64000) 
       {
          tax = (THIRD_MARRIED_VALUE + (taxIncome - 64000) * THIRD_MARRIED_TAX); 
       }
       else 
       {
          System.out.print (" -- illegal income --");
       }

       System.out.printf("Your income tax is $%.2f", tax);

 }
}

1 个答案:

答案 0 :(得分:2)

作为新程序员的好建议:

(1) Learn how to use the debugger,
(2) learn how to put in break points and investigate local variables and
(3) learn how to add and investigate logs. 

一旦你完成了这个,你就会发现这个问题出现了问题:

else if(status.equals("married")); 

你基本上已经终止了其他声明,这意味着其他人什么都不做,因此,你的税收&#34;该行覆盖了该值:

tax = taxIncome * FIRST_MARRIED_TAX;

更改else if ...以包装您的下一个语句{ },您就可以了。

//... (if condition)
else if (status.equals("married")) {
    if (taxIncome >= 0 && taxIncome <= 16000) {
        tax = taxIncome * FIRST_MARRIED_TAX;
    } else if (taxIncome > 16000 && taxIncome <= 64000) {
        tax = (SECOND_MARRIED_VALUE + (taxIncome - 16000) * SECOND_MARRIED_TAX);
    } else if (taxIncome > 64000) {
        tax = (THIRD_MARRIED_VALUE + (taxIncome - 64000) * THIRD_MARRIED_TAX);
    } else {
        System.out.print(" -- illegal income --");
    }
}