【问题标题】:Compiling Error: Missing return statement [duplicate]编译错误:缺少返回语句 [重复]
【发布时间】:2013-10-27 18:56:02
【问题描述】:

这个程序用 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) ;
    }
}

【问题讨论】:

  • 您需要为程序中的每个执行路径提供返回

标签: java if-statement methods compilation return


【解决方案1】:

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

while 循环结束后提供return 语句,或者如果代码真的不应该出现在此处,则抛出某种Exception (IllegalStateException?)。

【讨论】:

    【解决方案2】:

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

    它将使它编译并且您的代码仍然可以工作。

    【讨论】:

      【解决方案3】:

      只有return 语句位于if 块的主体内。

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

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

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

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

      【讨论】:

      • 所以我想做的是,如果这两个条件都不起作用,其他任何事情都会让骰子再次滚动。应该是什么样的return语句?
      【解决方案4】:

      您的 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.**;
      

      【讨论】:

        【解决方案5】:
        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;
        }
        

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

        【讨论】:

          【解决方案6】:

          当您必须添加 return 时,请确保为添加 default return 语句的每个可能的执行路径添加 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
                  }
                  }
          

          【讨论】:

          • 所以我想做的是,如果这两个条件都不起作用,其他任何事情都会让骰子再次滚动。应该是什么样的return语句?
          • Nothing 只是在do-while 循环之后添加一个default return 语句来告诉编译器如果没有任何效果return false
          【解决方案7】:

          这是您的代码的一个非常精简的版本:

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

          如果condition1condition2 为真,playOneGame() 将返回真或假。但是,如果condition1condition2 为假,那么唯一可以运行的代码就是code3。 code3 不包含 return 语句,因此理论上playOneGame() 可能不会返回任何内容。 知道condition1condition2 永远不会都是假的,但是java 编译器不会,所以它会抛出一个编译器错误。如果它没有引发编译器错误并且condition1condition2 都以某种方式变为假,那么它将引发运行时错误。运行时错误比编译器错误更难调试,因此编译器通过抛出一个易于修复的错误来帮您一个忙。

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

          【讨论】:

            【解决方案8】:

            在您的 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 语句,以便于阅读。您可能还想养成这样做的习惯,以便其他人可以更轻松地理解您的代码。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-08-20
              • 2013-03-21
              • 2016-01-04
              • 2015-06-01
              • 2021-05-27
              • 2014-01-09
              • 1970-01-01
              • 2020-05-28
              相关资源
              最近更新 更多