简体   繁体   中英

Why do i need an additional return statement in a method when using if-else and all possible conditions are met

My question is about return statements in a method. In the method calculatePrice below it accepts an integer as a parameter and the if-else block tests for every possible case (below 0, within the ranges, and at the top of the range) and has a return statement for each. The code below compiles with an error because I don't include a return statement at the bottom, but I'm confused why I need that since it will meet one of the conditions before it ever gets to that last return. Thanks!

import java.text.DecimalFormat;
public class ElectricityCostSolution {
    public static void main(String[] args) {
        DecimalFormat price = new DecimalFormat("$##.00");
        int test1 = 984;
        int test2 = 2984;
        int test3 = 5984;
        System.out.println("The price for " + test1 + "kwh of electricity is " + price.format(calculatePrice(test1)));
        System.out.println("The price for " + test2 + "kwh of electricity is " + price.format(calculatePrice(test2)));
        System.out.println("The price for " + test3 + "kwh of electricity is " + price.format(calculatePrice(test3)));
    }
    public static double calculatePrice(int hours) {
        final double LEVEL_ONE_PRICE = .0577;
        final double LEVEL_TWO_PRICE = .0532;
        final double LEVEL_THREE_PRICE = .0511;
        if (hours <= 1000) {
            return hours * LEVEL_ONE_PRICE;
        } else if (hours > 1000 && hours <= 5000) {
            return (1000 * LEVEL_ONE_PRICE) + (hours - 1000) * LEVEL_TWO_PRICE;
        } else if (hours > 5000) {
            return (1000 * LEVEL_ONE_PRICE) + (4000 * LEVEL_TWO_PRICE) + (hours - 5000) * LEVEL_THREE_PRICE;
        }
    }
}

Why do you need that 3rd if? It's always true.

The compiler will not actually evaluate all your conditions to see if both decisions can really happen. In general, it's not possible to make these sorts of determinations automatically.

If you make the last condition an else instead of an "else if", then you don't need another return.

That is because you have added if and else if statements, there is no return statement for the scenario when any of these conditions aren't met.

Perhaps you should add an else block of code or simply add one return statement at the end of the code or you can just change your existing code to following

    if(hours <= 1000)
    {
      return hours * LEVEL_ONE_PRICE;
    }
    else if(hours>1000 && hours <= 5000)
    {
      return (1000 * LEVEL_ONE_PRICE) + (hours-1000) * LEVEL_TWO_PRICE;
    }
    else 
    {  
     return (1000 * LEVEL_ONE_PRICE) + (4000 * LEVEL_TWO_PRICE) + (hours - 5000) * LEVEL_THREE_PRICE;
    }

Maybe it's because Java doesn't know that all conditions are met. When you compile, it looks for code errors, but I don't think it makes sure your if-else statements are all met. Just in case, it makes you add a return false; or return true; at the end to make sure that there are no problems. I think it is just that the developers don't want to add a check to Java. Another example of this is setting a variable equal to something else. Shouldn't it know the difference between var1 = var2 and if (var1 = var2) ?

Correct me if I'm wrong.

-Ben

It is because the compiler is not checking your actual conditions, so it warns that if none of your conditions are satisfied then you won't have a return value. That's why very often programming teachers say that a good practice is to store your "return" value in a variable and then actually return it at the end of the procedure. Because of that and because it is easier to debug. For your personal issue, you can just omit the if (hours > 5000) statement and leave it to the last else . You will obtain the same algorithm (since > 5000 is the case which will satisfy if none of the other does) and the compiler will recognize all the execution paths so it won't throw a warning.

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