class PowerRec
{
   static double powRec(double x, int power)
   {
      if (power == 1){
        return x;
      }
      return x * powRec(x, power - 1);
   }

  public static void main(String args[])
  {
    double x = 2;
    System.out.println (x + " to the fourth is " + powRec (x, 4));
  }
}

最佳答案

您的代码中连续有两个返回语句。错误的powRec方法

 static double powRec(double x, int power) {
    if (power == 0) {

        return 1;


    }
    return x * powRec(x, power - 1);
}

关于java - 函数powRec(x,n-1)如何执行幂运算?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48675771/

10-10 03:16