-1

编译器说该语句在该语句所在的行上是不可访问的if。我对Java不太熟悉。

public double calculate()
{
  total_usage_charge = getUsageCharge();
  total_charge = rate + total_usage_charge;

  return total_charge;

  if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95)
  {
    sB = getUsageCharge() - 14.95;
    System.out.println("You're spending more money than you should. If you switched to Plan B you would save:$" + sB);
  }
}
4

8 回答 8

6

因为你通过执行这个从方法返回:

return total_charge;

所以下一条语句永远不会被执行。

于 2013-03-19T08:04:29.303 回答
3

您在语句return total_charge;之前从您的方法返回 ( )。ifa 之后的任何代码return都无法执行(finally如果您的 return 语句位于 a 中,则相关块除外try...catch...finally)。

于 2013-03-19T08:04:13.830 回答
2

您在调用 if 语句之前返回,因此它无法访问。

方法在到达 return 语句时返回到调用它的代码。

于 2013-03-19T08:04:47.923 回答
1
return total_charge;

您的方法此时返回,发布任何代码都无法访问。

于 2013-03-19T08:05:06.460 回答
1

您已将 return 语句置于 IF 之上。因此,当编译器每次从那里返回时都会出现此语句并且如果无法随时执行,请删除返回语句或将其放在 if 条件下方。

于 2013-03-19T08:08:12.430 回答
1

返回语句后代码将不会执行。以下代码块导致问题,即

return total_charge;

所以你必须删除这条线或把它放在最后。!

于 2013-03-19T08:09:25.547 回答
0
public double calculate()
{
  total_usage_charge = getUsageCharge();
  total_charge = rate + total_usage_charge;

  if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95)
  {
    sB = getUsageCharge() - 14.95;
    System.out.println("You're spending more money than you should. If you switched to Plan B you  would save:$" + sB);
  }

  return total_charge;
}

就这样;)

于 2013-03-19T08:08:42.660 回答
-1
**if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95)
        {
            sB = getUsageCharge() - 14.95;
            System.out.println("You're spending more money than you should. If you switched to Plan B you would save:$" + sB);
        }
        else if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 19.95)
        {
            sC = getUsageCharge() - 19.95;
            System.out.println("You're spending more money than you should. If you switched to Plan C you would save:$" + sC);
        }
        else if("B".equals(package_plan.toUpperCase()) && hours < 10)
        {
            sA = getUsageCharge() - 9.95;
            System.out.println("You're spending more money than you should. If you switched to Plan A you would save:$" + sA);
        }
        else if("B".equals(package_plan.toUpperCase()) && getUsageCharge() > 19.95)
        {
            sC = getUsageCharge() - 19.95;
            System.out.println("You're spending more money than you should. If you switched to Plan C you would save:$" + sC);**
        }**

你是不是想评论它,如果是的话,做对了。或者根据提供的代码,这是错误和无法访问的语句。

评论它/* your code to comment */

于 2013-03-19T08:07:31.953 回答