1 java methods if-statement compilation return
这个程序用3种不同的方法来制作掷骰子.我需要帮助玩掷骰子,但我需要有这3种不同的方法,但由于某些原因,每次我编译我收到此错误:
CrapsAnalysis.java:48: error: missing return statement
}
^
1 error
Process javac exited with code 1
Run Code Online (Sandbox Code Playgroud)
码:
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) ;
}
}
Run Code Online (Sandbox Code Playgroud)
Java必须查看所有执行路径.如果while
循环结束而没有返回任何内容会发生什么 您可能在逻辑上阻止了这一点,但Java编译器不会进行该分析.
return
在while
循环结束后提供一个语句,或者抛出某种Exception
(IllegalStateException
?)如果代码真的不应该在那里.