-1

The compiler is saying that the statement is unreachable on the line with the if statement. I'm not overly familiar with 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

Because you're returning from the method by executing this:

return total_charge;

So the next statement is never going to be executed.

于 2013-03-19T08:04:29.303 に答える
3

You return (return total_charge;) from your method just before the if statement. No code after a return can ever be executed (except the relevant finally block if your return statement is located in a try...catch...finally).

于 2013-03-19T08:04:13.830 に答える
2

You are returning before calling if statement so its unreacheable .

A method returns to the code that invoked it when it reaches a return statement.

于 2013-03-19T08:04:47.923 に答える
1
return total_charge;

You method returns at this point, post which any code won't be reachable.

于 2013-03-19T08:05:06.460 に答える
1

you have putted return statement above IF. so when compiler comes on this statement everytime it returns from there and if can not executed anytime so, remove returns statement or put it below if Condition.

于 2013-03-19T08:08:12.430 に答える
1

Code will not be executed after return statement.following block of code is causing problem i.e.

return total_charge;

So you will have to remove this line or put it at the end.!

于 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;
}

Just like that ;)

于 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);**
        }**

Did u meant to comment it, if yes do it right. or this is error and unreachable statement according to the code provided.

Comment it with /* your code to comment */

于 2013-03-19T08:07:31.953 に答える