Java 编译错误:缺少 return 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19457991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling Error: Missing return statement
提问by Angelo Mejia
This program plays craps with 3 different methods. I need help in playing craps but i'm required to have these 3 different methods but for some reason every time I compile I am getting this error:
该程序使用 3 种不同的方法玩掷骰子。我在玩掷骰子时需要帮助,但我需要使用这 3 种不同的方法,但出于某种原因,每次编译时我都会收到此错误:
CrapsAnalysis.java:48: error: missing return statement
}
^
1 error
Process javac exited with code 1
Code:
代码:
public class CrapsAnalysis
{
public static int rollDie( int n) {
return (int)(Math.random()*n) + 1 ;
}
public static int rollDice( ) {
return rollDie(6) + rollDie(6) ;
}
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7) ;
}
}
回答by Sam I am says Reinstate Monica
you only return
statements are inside the body of an if
block.
你只有return
语句在if
块的主体内。
The compiler doesn't know if any of those if blocks will ever be reached, so it's giving you an error.
编译器不知道是否会到达这些 if 块中的任何一个,因此它会给您一个错误。
You might want to have a default return statement at the end
您可能希望在最后有一个默认的 return 语句
} while (rollDice() != playerPoint || rollDice() != 7) ;
return false;
}
I'm assuming that if that default return statement ever actually gets executed, than it's an error state, and you should react accordingly.
我假设如果那个默认的 return 语句真的被执行了,那么它就是一个错误状态,你应该做出相应的反应。
回答by rgettman
Java must look at all execution paths. What happens if the while
loop ends without returning anything? You may be logically preventing that, but the Java compiler won't do that analysis.
Java 必须查看所有执行路径。如果while
循环结束而不返回任何内容会发生什么?您可能在逻辑上阻止了这种情况,但 Java 编译器不会进行这种分析。
Provide a return
statement after the end of the while
loop, or throw some kind of an Exception
(IllegalStateException
?) if the code really shouldn't ever make it there.
return
在while
循环结束后提供一个语句,或者如果代码真的不应该出现在那里,则抛出某种Exception
(IllegalStateException
?)。
回答by dtgee
After your while loop you need a return statement, because the if/else statements may or may not be executed. If none of them execute, you have no return. Therefore, you need to ensure that there will be at least one return statement that can always be executed.
在 while 循环之后,您需要一个 return 语句,因为 if/else 语句可能会也可能不会执行。如果他们都没有执行,你就没有回报。因此,您需要确保至少有一个始终可以执行的 return 语句。
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7) ;
return false;
}
Also, for clarity, I rearranged your if/else statements so it is easier to read. You might want to get into the habit of doing this as well so that others can have an easier time understanding your code.
此外,为了清楚起见,我重新排列了您的 if/else 语句,以便于阅读。您可能还想养成这样做的习惯,以便其他人可以更轻松地理解您的代码。
回答by Brian
You are missing a return
in your while block:
您return
的 while 块中缺少一个:
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7)
**// You are missing a return statement here.**;
回答by Tim
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7) ;
return SOMETHING HERE;
}
You should look into consistent code formatting. Not only for us, but for yourself as well so when you look at this code after a few weeks you can still read it.
您应该研究一致的代码格式。不仅为我们,也为您自己,所以当您在几周后查看此代码时,您仍然可以阅读它。
回答by Martin
You can add the last return statement after your code like this:
您可以在代码后添加最后一个 return 语句,如下所示:
public static boolean playOneGame() {
int newDice = rollDice();
int roll = rollDice(); // first roll of the dice
int playerPoint = 0; // player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;
else if (roll == 2 || roll == 3 || roll == 12)
return false;
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;
else if (rollDice() == playerPoint)
return true;
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7);
return false;
}
It will make it compile and your code will still work.
它将使其编译并且您的代码仍然可以工作。
回答by Prateek
When you have to add return make sure you add return
for every possible execution path of add a default return
statement. You program is missing both of them
当您必须添加 return 时,请确保return
为添加default return
语句的每个可能的执行路径添加。你的程序缺少他们两个
public class CrapsAnalysis
{
public static int rollDie( int n) {
return (int)(Math.random()*n) + 1 ;
}
public static int rollDice( ) {
return rollDie(6) + rollDie(6) ;
}
public static boolean playOneGame( ) {
int newDice = rollDice();
int roll = rollDice(); //first roll of the dice
int playerPoint = 0; //player point if no win or loss on first roll
if (roll == 7 || roll == 11)
return true;<--- Works
else if (roll == 2 || roll == 3 || roll == 12)
return false;<--- Works
else
playerPoint = roll;
do {
if (rollDice() == 7)
return false;<--- Works
else if (rollDice() == playerPoint)
return true;<--- Works
else
newDice = rollDice();
} while (rollDice() != playerPoint || rollDice() != 7) ;
//No return here. You need to add a default return if none of the conditions above satisfies
}
}
回答by The Guy with The Hat
Here is a very stripped down version of your code:
这是一个非常精简的代码版本:
public static boolean playOneGame()
{
if(condition1 == true)
{
//code1
return true;
}
else if(condition2 == true)
{
//code2
return false;
}
else
{
//code3
}
//code4
}
If condition1
or condition2
is true, playOneGame()
will return either true of false. However, if condition1
and condition2
, are bothfalse, the only code that will run is code3. code3 does not contain a return statement, so it is theoretically possible that playOneGame()
will not return anything. Youknow that condition1
and condition2
will never both be false, but the java compiler does not, so it throws a compiler error. If it did not throw a compiler error and condition1
and condition2
somehow both became false, it would throw a runtime error. Runtime errors are a lot harder to debug than compiler errors, so the compiler is doing you a favor by throwing an easy-to-fix error.
如果condition1
或condition2
为真,playOneGame()
将返回真或假。但是,如果condition1
和condition2
都为假,则将运行的唯一代码是 code3。code3 不包含 return 语句,因此理论上可能playOneGame()
不会返回任何内容。您知道condition1
并且condition2
永远不会都为假,但 java 编译器不会,因此它会引发编译器错误。如果没有抛出一个编译错误,并condition1
和condition2
莫名其妙地都成为虚假的,它会引发运行时错误。运行时错误比编译器错误更难调试,所以编译器通过抛出一个易于修复的错误来帮助你。
To fix the missing return statement, add a return statement to code3 or code4.
要修复缺少的 return 语句,请将 return 语句添加到 code3 或 code4。