0

好的。所以这里的情况。我的班级中有一个 calculateTax 方法,如下所示。

但我意识到我犯了一个很大的错误。“税”实际上是在课程开始时创建的一个字段。

private double tax;

这实际上应该是一个变量!

但是,当我在方法中将它创建为局部变量时,public double calculateTax(double tax)它可以工作,但是当我针对它运行测试时,它们会失败。我收到消息

The method calculateTax(double) in the type Salary is not applicable for the arguments ()

那么我哪里错了?

如何在不更改方法名称的情况下创建 tax 变量(仍然可以返回)?“calculateTax”方法需要保持不变。那么我在哪里以及如何创建“税”变量?提前致谢!

public double calculateTax() {

if (this.salary <= personalAllowance) { // If the salary is less
  // £9440 (personal allowance) and below then no tax will be applied.

}

else if (this.salary <= taxThreshold) { // Else if the salary is less than or equal to the
  // tax threshold then do the following:
  double taxableSalary = this.salary - personalAllowance; // Salary take away the personal allowance
  // equals the taxable salary.
  this.tax = taxableSalary * 0.2; // The tax equal the taxable salary * 0.2 (20%)
}

else if (this.salary > 32010) {

  double basicRate = taxThreshold * 0.2; // The basic rate tax is the tax threshold * 0.2
  double difference = this.salary - taxThreshold; // The difference is the salary - the tax threshold
  double highTax = difference - personalAllowance; // The high tax to be calculated is the difference
  // take away personal allowance.
  double highRate = highTax * 0.4; // The high rate tax is the high tax * the high tax value (40%)

  this.tax = highRate + basicRate; // Total tax is the high rate tax (40%) + the basic rate tax (20%)

}
return tax;
4

1 回答 1

0

您的税收变量很好。

编译器认为您的calculateTax()方法需要一个参数,一个双精度值,表示薪水。(我认为-也许是税率?)例如 calculateTax(56789.12)

我在您发布的代码中看不到这一点,但有些内容不完整。

于 2013-10-16T16:55:43.097 回答