为什么我的代码不断要求丢失return语句

时间:2013-12-18 14:25:50

标签: java return

  public double open() {
    if (status.equalsIgnoreCase("Pending")) {
        status = "Open";
        if (startPrice <= 5) {
            return 0.2;
        } else if (startPrice > 5 && startPrice <= 20) {
            return 0.5;
        } else if (startPrice > 20 && startPrice <= 100) {
            return 1;
        } else if (startPrice > 100 && startPrice <= 250) {
            return 2.5;
        } else if (startPrice > 250) {
            return 5;
        }
    } else if (!status.equals("Pending")) {
        return -1;
    }           
}

你能解释一下为什么编译器不断要求丢失返回语句。我该如何解决呢?

8 个答案:

答案 0 :(得分:3)

编译器不知道status.equalsIgnoreCase("Pending")!status.equals("Pending")涵盖了所有可能性。事实上,我只是通过检查函数名称来猜测。

你需要帮助它。

一个修复 - 如果这是你的意图 - 是写

 } else /*if (!status.equals("Pending"))*/ {
     return -1;
 } 

答案 1 :(得分:2)

这样做

public double open() {
   if (status.equalsIgnoreCase("Pending")) {
        status = "Open";
        if (startPrice <= 5) {
            return 0.2;
        } else if (startPrice > 5 && startPrice <= 20) {
            return 0.5;
        } else if (startPrice > 20 && startPrice <= 100) {
            return 1;
        } else if (startPrice > 100 && startPrice <= 250) {
            return 2.5;
        } else if (startPrice > 250) {
            return 5;
        }
    }
    return -1;
}      

答案 2 :(得分:1)

这是抱怨,因为你的所有回复陈述都在if statements。只需删除else if并在结尾处返回-1。

答案 3 :(得分:1)

如果两者都没有

  1. status.equalsIgnoreCase( “挂起”)
  2. !status.equals( “挂起”)
  3. 是真的吗?

    您还需要一个退货声明。

答案 4 :(得分:1)

Java不够聪明,无法确定您是否能够保证返回。你需要通过在ifs上有一个else来明确告诉它,或者在最后返回以捕获任何流量。

在这种情况下,您的if语句也是多余的。清理后的工作版本如下:

public double open() {
    if (status.equalsIgnoreCase("Pending")) {
        status = "Open";
        if (startPrice <= 5) {
            return 0.2;
        } else if (startPrice <= 20) {
            return 0.5;
        } else if (startPrice <= 100) {
            return 1;
        } else if (startPrice <= 250) {
            return 2.5;
        } else {
            return 5;
        }
    }
    return -1;          
}

else if (startPrice > 5 && startPrice <= 20)当你到达这一行时,你已经知道startPrice > 5。如果不是那么它就会进入前一个if块。

答案 5 :(得分:0)

这是因为如果:

status.equals("Pending")

返回true,你什么都不返回。

您可以使用:

  if (status.equalsIgnoreCase("Pending")) {
      ....
  } else {
      return -1;
  }

答案 6 :(得分:0)

将上一个if else更改为else,以提供后备解决方案。您正在使用if提供另一个if else条件,因此如果此条件为假,则没有分支。

答案 7 :(得分:0)

这与返回语句本地的内容有关。如果所有if语句都是false,则该方法不会返回任何内容,因为所有return语句都是if语句的本地语句。程序无法知道状态是否会挂起,因此编译器会抱怨。

public double open() {
    if (status.equalsIgnoreCase("Pending")) {
        status = "Open";
        if (startPrice <= 5) {
            return 0.2;
        } else if (startPrice > 5 && startPrice <= 20) {
            return 0.5;
        } else if (startPrice > 20 && startPrice <= 100) {
            return 1;
        } else if (startPrice > 100 && startPrice <= 250) {
            return 2.5;
        } else if (startPrice > 250) {
            return 5;
        }
    return -1; // no need for an else statement since if an one if statement returned true it wouldn't execute the rest of the code
}