在Java中编写递归方法时遇到问题

时间:2021-08-24 22:23:49

Here is my assignment:

这是我的任务:

Write a program that contains a recursive method to compute the following:

编写一个包含递归方法的程序来计算以下内容:

m(i) = 1/2 + 2/3 +...i/i+1

The main method should display:

主要方法应该显示:

 ____________________
| i            m(i)  | 
|                    |
| 1   ------    0.5  |
|                    |
| 2   ------  1.1667 |
|                    |
|     ......         |
|                    | 
| 19  ------  16.4023|
|                    |
| 20  ------  17.3546|
|____________________|     

Here is what I have so far. I'm fairly new to programming and having some trouble understanding recursive methods. Any advice is appreciated. Thanks!

这是我到目前为止所拥有的。我对编程很新,在理解递归方法时遇到一些麻烦。任何建议表示赞赏。谢谢!

public class RecursionMethod
{
    public static void main (String[] args)
    {
        System.out.println("i\tm(i)\n--------------");
        for (int i = 1; i <= 20; i++)
        {
             System.out.println(m(i));
        }
    }

    public static double m(int x)
    {
        if (x==1)
            return .5;
        else
            return m(x/x+1);  
    }
} 

3 个解决方案

#1


This is what you were looking for: The recursive part here is to get the value for a particular value for i, and then recursively call for i-1.

这就是你要找的东西:这里的递归部分是获取i的特定值的值,然后递归调用i-1。

public static double resursiveSum(int x) {
    if (x == 1) {
        return .5;
    } else {
        return ((double)x / (double)(x + 1)) + resursiveSum(x - 1);
    }
}

#2


The idea is to add the current element of the series to the rest of the series (which you get from the recursive call) :

我们的想法是将系列的当前元素添加到系列的其余部分(您从递归调用中获得):

  public static double m(int x)
  {
   if (x==1)
      return .5;
   else
      return (double)x/(x+1) + m(x-1);  
  }

Note that the casting to double is important, since without it you'll be doing int division, which will return 0.

注意,转换为double是很重要的,因为如果没有它,你将进行int除法,它将返回0。

#3


This should work..give it a try...

这应该工作..尝试一下......

  public class RecursionMethod
{
   public static void main (String[] args)
{
  System.out.println("i\tm(i)\n--------------");
  for (int i = 1; i <= 20; i++)
  {
     System.out.println(i +"---+---"+m(i));
  }
}

  public static double m(int x)
  {
   if (x==1)
      return .5;
   else if (x==0)
       return 0;
   else

      return ((double)x/(double)(x+1)+m(x-1));  
  }
} 

Where you went wrong was here return m(x/x+1); With this statement you basically created a stack explosion... With recursion your aim should be reaching the base condition ie if(i==1) statement..The only way to reach this is by decrementing the value of i until you reach i==1 and thus rewinding the stack from there. Hope this Helps

你出错的地方在这里返回m(x / x + 1);使用这个语句你基本上创建了一个堆栈爆炸...随着递归,你的目标应该是达到基本条件,即if(i == 1)语句。达到这个的唯一方法是递减i的值,直到你到达i == 1然后从那里倒回堆栈。希望这可以帮助

Nice try Evan

很好的尝试埃文

#1


This is what you were looking for: The recursive part here is to get the value for a particular value for i, and then recursively call for i-1.

这就是你要找的东西:这里的递归部分是获取i的特定值的值,然后递归调用i-1。

public static double resursiveSum(int x) {
    if (x == 1) {
        return .5;
    } else {
        return ((double)x / (double)(x + 1)) + resursiveSum(x - 1);
    }
}

#2


The idea is to add the current element of the series to the rest of the series (which you get from the recursive call) :

我们的想法是将系列的当前元素添加到系列的其余部分(您从递归调用中获得):

  public static double m(int x)
  {
   if (x==1)
      return .5;
   else
      return (double)x/(x+1) + m(x-1);  
  }

Note that the casting to double is important, since without it you'll be doing int division, which will return 0.

注意,转换为double是很重要的,因为如果没有它,你将进行int除法,它将返回0。

#3


This should work..give it a try...

这应该工作..尝试一下......

  public class RecursionMethod
{
   public static void main (String[] args)
{
  System.out.println("i\tm(i)\n--------------");
  for (int i = 1; i <= 20; i++)
  {
     System.out.println(i +"---+---"+m(i));
  }
}

  public static double m(int x)
  {
   if (x==1)
      return .5;
   else if (x==0)
       return 0;
   else

      return ((double)x/(double)(x+1)+m(x-1));  
  }
} 

Where you went wrong was here return m(x/x+1); With this statement you basically created a stack explosion... With recursion your aim should be reaching the base condition ie if(i==1) statement..The only way to reach this is by decrementing the value of i until you reach i==1 and thus rewinding the stack from there. Hope this Helps

你出错的地方在这里返回m(x / x + 1);使用这个语句你基本上创建了一个堆栈爆炸...随着递归,你的目标应该是达到基本条件,即if(i == 1)语句。达到这个的唯一方法是递减i的值,直到你到达i == 1然后从那里倒回堆栈。希望这可以帮助

Nice try Evan

很好的尝试埃文

相关文章