Java 所得税计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22189347/
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
Income tax calculator
提问by user3344737
package edu.westga.taxcalculator.model;
/**
* Creates a taxReturn object
*/
public class TaxReturn {
private double income;
/**
* Constructor for the TaxReturn class
*
* @param income
* the income of the person.
*/
public TaxReturn(double income) {
if (income < 0) {
throw new IllegalArgumentException(
"Income can't be less than zero.");
}
this.income = income;
}
public void getTax() {
if (income <= 50000) {
income *= 0.01;
} else if (income <= 75000) {
income *= 0.02;
} else if (income <= 100000) {
income *= 0.03;
} else if (income <= 250000) {
income *= 0.04;
} else if (income <= 500000) {
income *= 0.05;
} else
income *= 0.06;
}
}
package edu.westga.taxcalculator.controller;
import java.util.Scanner;
import edu.westga.taxcalculator.model.TaxReturn;
public class TaxCalculatorController {
public static void main(String[] args) {
System.out.println("Please enter your income: ");
Scanner theScanner = new Scanner(System.in);
double income = theScanner.nextDouble();
TaxReturn theCalculator = new TaxReturn(income);
System.out.println("The amount of tax is: " + taxReturn.getTax());
}
}
I am writing a program for an income tax calculator and the project has a class and a tester class. It is suppose to calculate the income tax of the amount I enter but it is not working out so well. I would appreciate any help because I am definitely stuck on this.
我正在为所得税计算器编写程序,该项目有一个类和一个测试类。假设要计算我输入的金额的所得税,但结果不是很好。我将不胜感激,因为我绝对坚持这一点。
采纳答案by Scary Wombat
For a start change
开始改变
TaxReturn theCalculator = new TaxReturn(income);
System.out.println("The amount of tax is: " + taxReturn.getTax());
to
到
TaxReturn theCalculator = new TaxReturn(income);
System.out.println("The amount of tax is: " + theCalculator .getTax());
Also your Constructor
throws an Exception
, but does not declare that it is going to.
你也Constructor
抛出一个Exception
,但没有声明它会。
so change to
所以改为
public TaxReturn(double income) throw IllegalArgumentException { ....