Beginners Java (Loops) - Missing return statement

2020-05-02 10:39发布

问题:

import java.util.Scanner;

public class Vending {
    public double vend(double balance) {
        Scanner in = new Scanner(System.in);
        balance = 0;
        System.out.print("Enter a command = ");
        String command = in.nextLine();

        while (in.hasNext()) {
            if (command.equals("penny")) {
                balance = balance + 0.01;
                System.out.println("balance = " + balance);
            }
            return balance;
        }
    }
}

Hi! I have tried everything to figure out why the return statement is not being recognized. If I put the "return balance" anywhere else it says that the system.out.println is unreachable... Can any of you kindly help me out as to why this may not be working?? Thank you!!

回答1:

The return statement is inside the while loop and outside the if condition segment, so the code its breaking the loop on the first iteration.

Possible solution: move the return outside the while

while (in.hasNext()) {
    if (command.equals("penny")) {
        balance = balance + 0.01;
        System.out.println("balance = " + balance);
    }
}
return balance;


回答2:

Your method vend(double balance) is defined to return a double. Whatever happens within the method, it must return a double. Now you have a return statement here:

while (in.hasNext()) {
    if (command.equals("penny")) {
        balance = balance + 0.01;
        System.out.println("balance = " + balance);
    }
    return balance;
}

But what happens if your statement in.hasNext() returns false? Then the return inside it will never be reached. Therefore the compiler can not guarantee that your method is valid java and is therefore complaining.

You should add a return statement outside the while.

When you say

If I put the "return balance" anywhere else it says that the system.out.println is unreachable

You put the return statement right before a System.out.println() statement. When a method returns, it gives up on anything that might've happened after the return statement, basically making anything after it useless. The return statement must be the last statement in some execution branch of your method.



回答3:

That's because the compiler can't guarantee that the return statement will execute because it is within your loop. It has to assume that in.hasNext() may return false and never hit that return.



回答4:

You're getting the "Missing return statement" error because there is a possiblity that your while loop is never executed. This leads to the program reaching the end of the function without ever returning.

while (in.hasNext()) {
  if (command.equals("penny")) {
    balance = balance + 0.01;
    System.out.println("balance = " + balance);
  }
  return balance;
}

If we pretend in never has any lines then the program passes the while and therefore the return causing this issue. For your case you can probably just have a default return such as return 0; or however you'd like to handle the situation.