简体   繁体   中英

Why is it saying my If statement is unreachable?

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

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

return total_charge;

So the next statement is never going to be executed.

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 ).

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.

return total_charge;

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

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.

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

return total_charge;

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

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 ;)

**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 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM