编译错误:缺少return语句

时间:2013-10-18 19:55:20

标签: java if-statement methods compilation return

这个程序使用3种不同的方法来玩掷骰子。我需要帮助玩掷骰子,但我需要有这3种不同的方法,但由于某些原因我每次编译时都会收到此错误:

CrapsAnalysis.java:48: error: missing return statement
    }
    ^
1 error
Process javac exited with code 1

代码:

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) ;
    }
}

8 个答案:

答案 0 :(得分:5)

Java必须查看所有执行路径。如果while循环结束而没有返回任何内容会发生什么?您可能在逻辑上阻止了这一点,但Java编译器不会进行该分析。

return循环结束后提供while语句,或者抛出某种ExceptionIllegalStateException?),如果代码真的不应该把它放在那里。

答案 1 :(得分:3)

您可以在代码之后添加最后一个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;
}

它将使其编译,您的代码仍然有效。

答案 2 :(得分:2)

您只有return个语句位于if块的正文中。

编译器不知道是否会到达任何块if,所以它会给你一个错误。

您可能希望在结尾处有一个默认的return语句

    } while (rollDice() != playerPoint || rollDice() != 7) ;
    return false;
}

我假设如果那个默认的return语句实际上被执行了,那么它就是一个错误状态,你应该做出相应的反应。

答案 3 :(得分:2)

您在while块中缺少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) 
     **// You are missing a return statement here.**;

答案 4 :(得分:2)

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;
}

您应该研究一致的代码格式。不仅对我们而言,对你自己也是如此,所以当你在几周后查看这段代码时,你仍然可以阅读它。

答案 5 :(得分:2)

当您必须添加返回时,请确保为添加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
        }
        }

答案 6 :(得分:1)

以下是您的代码的精简版本:

public static boolean playOneGame()
{
    if(condition1 == true)
    {
        //code1
        return true;
    }
    else if(condition2 == true)
    {
        //code2
        return false;
    }
    else
    {
        //code3
    }
    //code4
}

如果condition1condition2为真,则playOneGame()将返回true为false。但是,如果condition1condition2 为false,则唯一将运行的代码是code3。 code3不包含return语句,因此理论上playOneGame()不会返回任何内容。 知道condition1condition2永远不会都是假的,但java编译器不会,所以它会抛出编译器错误。如果它没有抛出编译器错误并且condition1condition2两者都变为false,则会抛出运行时错误。运行时错误比编译器错误更难调试,因此编译器通过抛出易于修复的错误来帮助您。

要修复缺少的return语句,请将return语句添加到code3或code4。

答案 7 :(得分:0)

在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;
}

另外,为了清楚起见,我重新安排了你的if / else语句,以便更容易阅读。您可能也想养成这样做的习惯,以便其他人可以更轻松地理解您的代码。