Java 编译器说缺少return语句,但我已经有3个了
这太奇怪了。我的编译器说我缺少一个return语句,但我已经有3个了。 这是我的密码:Java 编译器说缺少return语句,但我已经有3个了,java,Java,这太奇怪了。我的编译器说我缺少一个return语句,但我已经有3个了。 这是我的密码: public int tortoiseMoves() { int i = tGen(); if (i >= 1 && i <= 5) { int fastplod = 3; return fastplod; } if (i >= 6 && i <= 8) {
public int tortoiseMoves()
{
int i = tGen();
if (i >= 1 && i <= 5)
{
int fastplod = 3;
return fastplod;
}
if (i >= 6 && i <= 8)
{
int slowplod = 1;
return slowplod;
}
if (i >= 9 && i <= 10)
{
int slip = -6;
return slip;
}
}
public int-tortoiseMoves()
{
int i=tGen();
如果(i>=1&&i=6&&i=9&&i,则必须确保始终有一个返回值。如果所有条件都失败,则不返回任何内容
一个解决方案是链接您的if
语句,因为它们是独占的,并使用else
捕获所有其他情况
public int tortoiseMoves() {
int i = tGen();
if (i >= 1 && i <= 5)
{
int fastplod = 3;
return fastplod;
}
else if (i >= 6 && i <= 8)
{
int slowplod = 1;
return slowplod;
}
else if (i >= 9 && i <= 10)
{
int slip = -6;
return slip;
}
else {
// return something or throw exception
return 0;
}
}
public int-tortoiseMoves(){
int i=tGen();
如果(i>=1&&i=6&&i=9&&i必须至少有一个返回语句,但代码中没有这样的语句,如果所有if语句都为false,则代码不知道返回什么
我不知道这是否是一个更好的解决方案:
public int tortoiseMoves()
{
switch (tGen())
{
case 1:
case 2:
case 3:
case 4:
case 5: return 3; // slowplod
case 6:
case 7:
case 8: return 1; // fastplod
case 9:
case 10: return -6; // slip
default: return 0; // default value if not in range
}
}
解决方案2(查找表)://最快,无需使用带if的开关或分支
public int tortoiseMoves()
{
int[] moves = {3,3,3,3,3,1,1,1,-6,-6};
return moves[tGen()-1];
}
小心BondException的排列!您需要为代码中的每种可能情况添加一个返回语句。在这种情况下,所有的ifs
计算结果都会为false
,因此不会执行返回
语句。请尝试以下操作:
public int tortoiseMoves()
{
int i = tGen();
if (i >= 1 && i <= 5)
{
int fastplod = 3;
return fastplod;
}
if (i >= 6 && i <= 8)
{
int slowplod = 1;
return slowplod;
}
if (i >= 9 && i <= 10)
{
int slip = -6;
return slip;
}
return Something to return if none of the ifs is true
}
public int-tortoiseMoves()
{
int i=tGen();
如果(i>=1&&i=6&&i=9&&i=1&&i=6&&i=9&&i通常有两种方法可以避免此问题:
默认返回:
public int tortoiseMoves()
{
int i = tGen();
if (i >= 1 && i <= 5)
{
int fastplod = 3;
return fastplod;
}
if (i >= 6 && i <= 8)
{
int slowplod = 1;
return slowplod;
}
if (i >= 9 && i <= 10)
{
int slip = -6;
return slip;
}
//If code reaches here, you know none of the other returns were called
return someDefaultValue;
// OR
// throw new Exception("Invalid value generated by tGen(): " + i);
}
public int-tortoiseMoves()
{
int i=tGen();
如果(i>=1&&i=6&&i=9&&i=1&&i=6&&i只有一个if(…)
语句对于给定的函数运行可能是真的。因此,使用if/if-else/else
结构可能会更清晰。您可以将return
或throw
语句放在if
结构之外,但我认为将其放在else
结构中会更清晰
public int tortoiseMoves() {
int i = tGen(); // random: 1 <= i <= 10
if (i >= 1 && i <= 5) return 3; // fastplod
else if (i >= 6 && i <= 8) return 1; // slowplod
else if (i >= 9 && i <= 10) return -6; // slip
else throw new IllegalStateException(); // this shouldn't happen!
}
public int-tortoiseMoves(){
int i=tGen();//random:1警告:许多注释表明您的错误是由于没有告诉编译器如何处理所有条件造成的。但是,重要的是要意识到,即使您使用if
/if else
覆盖了所有可能的条件,编译器仍会抱怨
public boolean isPositive(int x){
if(x > 0) return true;
else if(x <= 0) return false;
// error: "This method must return a result of type boolean"
}
public boolean isPositive(int x){
如果(x>0),则返回true;
else if(x 0}
。但是,您仍然需要在else
内部或if
结构外部返回最后一个返回值。但是在我的if循环中有3个返回值,但可能没有人执行过,如果i>10或i<1,返回什么?if tGen()returns-1您的i条件都不满足,因此如果您不添加超过if语句的第四个return语句,则不会返回任何内容。如果if条件都不为真,则应返回什么内容?如果i==11,则将命中no return语句如果i<1
或i>10
,则会发生什么情况:i>10,其中f您的返回语句将执行。提示:无。编译器在编译此方法时不知道这一点。它无法编译,因为每个方法都需要返回一个值,如果它不是void,在这种情况下是integer,因此您需要返回if语句以外的内容。这更合理。另一种选择是以抛出结束代码>,如果到达方法末尾表示发生了不应该发生的事情。如果他们不能成为默认值怎么办?按照@ajb的话做,并在最后一个else语句或最后一个语句中抛出异常。如果您知道该值必须在一个if
语句中,那么您希望您的程序通知您若发生了意想不到的事情,我肯定会在这个问题上抛出异常选项,其他任何事情都可能导致以后很难检测到bugon@RichardTingle请随意编辑你认为合适的例外情况。老实说,我认为“永远不会出现”的例外情况我只是抛出一个运行时异常。如果它发生了,我需要修复它是一个编程错误,我只需要知道它。考虑到它永远不会发生,这只是一个倒退,它不值得花更多的时间去考虑that@RichardTingle我们不知道方法tGen()的位置
从中获取值。也可能是外部的,不受他的控制。你是对的,我是根据OP的评论做出假设的,他们“涵盖了所有可能的值”,但这到底意味着什么还不是100%清楚
public int tortoiseMoves() {
int i = tGen(); // random: 1 <= i <= 10
if (i >= 1 && i <= 5) return 3; // fastplod
else if (i >= 6 && i <= 8) return 1; // slowplod
else if (i >= 9 && i <= 10) return -6; // slip
else throw new IllegalStateException(); // this shouldn't happen!
}
public boolean isPositive(int x){
if(x > 0) return true;
else if(x <= 0) return false;
// error: "This method must return a result of type boolean"
}