【问题标题】:Why return should be last line of my method?为什么 return 应该是我方法的最后一行?
【发布时间】:2019-12-18 12:35:06
【问题描述】:

我正在尝试解决这个问题: Solve for square root. 这是我的代码:

public class Solution {
    public int sqrt(int A) {
        for(int i=0;i<A;i++)
        {
            if(A%i==i)
            {
                return i;
            }
        }
        for(int i=A;i>0;i--)
        {
            for(int j=0;j<A;j++)
            {
                if(A%j==j)
                {
                    return j;
                }

            }

        }
    } 
}

但是在我尝试在 Interview Bit 上运行它之后,它显示了一个错误:

./Solution.java:23: error: missing return statement
}
^

1 个错误

现在我什至无法运行我的代码。

【问题讨论】:

  • 如果for 循环或if 块都没有输入会怎样?
  • 不是你类的最后一行,而是你方法的最后一行public int sqrt(int A)...
  • 在这里写下整个问题。没有人会点击外部链接。

标签: java amazon root square


【解决方案1】:

你的方法应该有一个返回,这是规则。不一定在最后一行,但绝对不应该出现没有返回的情况。使用您的自定义算法,您的返回始终是有条件的,编译器不会提前知道这些条件是否会为真。

【讨论】:

    【解决方案2】:

    所以,问题是这样的:

    让我们假设 A 是一个负数(例如 -1):

    第一个循环体永远不会被执行,因为 i (0) 大于 A (-1)

    for(int i=0;i<A;i++)
    

    第二个循环体永远不会被执行,因为 i (-1) 小于 0。

    for(int i=A;i>0;i--)
    

    因此 - 我们到达了函数的末尾,即使函数声明为返回 int,也没有什么可以返回。

    基本上 - 所有路径都必须有 return 语句(除非函数是 void - 在这种情况下 return 语句可以是隐式的)

    【讨论】:

      【解决方案3】:

      如果您声明了一个期望返回类型的方法,那么您必须注意执行该方法中所有块的所有场景。无论方法正在执行的代码块是什么,它都必须返回所需类型的内容。

      在下面的代码中找到缺失的块:

      public class Solution {
      public int sqrt(int A) {
          for(int i=0;i<A;i++)
          {
              if(A%i==i)
              {
                  return i; // returning an int here which is correct
              } // what if the the 'if' condition is not met. Add a return type for else
          }
          for(int i=A;i>0;i--)
          {
              for(int j=0;j<A;j++)
              {
                  if(A%j==j)
                  {
                      return j;
                  } // same what if the condition is not met add a return in 'else' if required.
      
              }
      
          } // if you are not adding any return in the else block. which is not required in your current program then just add a default return to your function.
      } 
      }
      

      更多信息请查看this

      【讨论】:

        【解决方案4】:

        虽然您确实有两个return 语句,但编译器指定如果您的两个条件都不满足(false),它仍然没有返回任何内容。

        所以要理解,你必须问自己:如果这两个条件都是false,会发生什么? (条件为if (A % i == i)if (A % j == j)

        解决方案:

        public int sqrt(int A) {
            for (int i = 0; i < A; i++) {
                if (A % i == i) {
                    return i;
                }
            }
            for (int i = A; i > 0; i--) {
                for (int j = 0; j < A; j++) {
                    if (A % j == j) {
                        return j;
                    }
        
                }
            }
        
            return -1; //-1 or 0 is widely returned by default to represent a failed function
        }
        

        所以基本上,当创建一个期望返回某些东西的方法时,你必须在任何条件语句之外返回它。

        【讨论】:

          【解决方案5】:

          你的方法签名是:

          Integer sqrt(int A)
          

          这说明你的方法体必须返回一个整数。

          现在,在您的代码中,您正在返回值,但是当某些条件为真时。如果该条件不为真怎么办。编译器足够聪明地发现,在这种情况下,没有返回值,因此出现错误。

          【讨论】:

            【解决方案6】:

            Return 应该是方法的最后一行,而不是类。由于您已将该方法声明为“int 类型”,因此您应该返回一个 int 值。编译器无法确定“如果条件”是否在所有输入中都为真。所以你应该在方法的末尾放置一个 return 0。

            【讨论】:

              【解决方案7】:

              您无法确保方法的每个状态都有返回值。除此之外,您的代码没有意义。

              1.你在第一个 if 循环中除以零(不要这样做,这是禁止的)

              2.第二个for循环中的条件i &gt; 0总是为假。

              最后应该是这个样子

              public int sqrt(int A) {
                  for () {
                      if () {
                          return i;
                      }
                  }
                  for () {
                      for () {
                          if () {
                              return j;
                          }
                      }
              
                  }
                  return 0;
              }
              

              如果您只想对数字求平方,请使用 Math.pow 方法,不要重新发明轮子!

              int i = 2;
              int square = Math.pow(i, 2);
              

              【讨论】:

              • 我已应用您的代码,但出现错误:./Solution.java:23: 错误:不兼容的类型: 无法转换为 int 返回 null; ^ 1 个错误
              • 嘿你的方法不是整数类型,它是int,所以请写'return 0; ' 而不是返回 null。在你的代码中
              • @RohitMittal 看看我更新的答案(已经存在求平方的方法)
              • 你应该先阅读问题,点击链接。不允许使用预定义的方法。
              猜你喜欢
              • 2019-09-26
              • 2011-01-10
              • 1970-01-01
              • 1970-01-01
              • 2016-05-05
              • 2015-10-15
              • 1970-01-01
              • 2012-07-24
              • 1970-01-01
              相关资源
              最近更新 更多