为什么说我的If语句无法访问?

时间:2013-03-19 08:02:31

标签: java

编译器说该语句在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);
  }
}

8 个答案:

答案 0 :(得分:6)

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

return total_charge;

所以下一个声明永远不会被执行。

答案 1 :(得分:3)

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

答案 2 :(得分:2)

您在调用if语句之前返回,因此它不可缓存。

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

答案 3 :(得分:1)

return total_charge;

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

答案 4 :(得分:1)

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

答案 5 :(得分:1)

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

return total_charge;

所以你必须删除这一行或将其放在最后。!

答案 6 :(得分: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;
}

就像那样;)

答案 7 :(得分:-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 */

进行评论