将字段更改为if语句中的可返回局部变量

时间:2013-10-16 16:52:05

标签: java variables if-statement arguments

确定。所以情况就是这样。我在课堂上有一个calculateTax方法,如下所示。

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

private double tax;

这应该是一个变量!

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

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

那我哪里错了?

如何在不更改方法名称的情况下创建税收变量(仍然可以返回)?方法“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;

1 个答案:

答案 0 :(得分:0)

您的纳税变量很好。

编译器认为你的calculateTax()方法需要一个表示工资的参数double。 (我想 - 可能是税率?),例如calculateTax(56789.12);

我在你发布的代码中没有看到,但有些内容不完整。