当递归在一个方法中被调用两次时会发生什么? [英] What happens when recursion is called twice in a method?

查看:42
本文介绍了当递归在一个方法中被调用两次时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,现在我已经了解了递归的概念.我已经解决了一些基本的任务,但是当我遇到多重递归时,我非常迷茫.我曾多次尝试解决以下递归问题,但始终无法解决.

I'm pretty new to programming and I have now come to the concept of recursion. I have solved a few basic assignments but when I come to multible recursion I get terribly lost. I have tried to solve the following recursion several times but just cant get it right.

使用参数"ABCD"2调用递归方法,即recMethod("ABCD", 2);.

A recursive method is called with the arguments "ABCD" and 2, i.e. recMethod("ABCD", 2);.

public void recMethod( String str, int n ) {
  if( n >= 0 ) {
     recMethod(str, n – 1);
     System.out.print(str.charAt(n));
     recMethod(str, n – 1);
  }
}

有人能解释一下这是怎么回事吗?第一个递归调用真的让我很困惑.

Is there somebody out there who can explain what is going on? The first recursive call really confuses me.

推荐答案

对我来说理解递归的最好方法是将其逐行写在纸上.我现在为你的案子做了什么.

The best way to understand recursion as for me is to write it on the paper line by line. What I did for your case now.

请尝试做同样的事情,不要犹豫,提出更多问题,希望对您有所帮助!

Please try to do the same, and don't hesitate to ask more question, I hope it helps!

这篇关于当递归在一个方法中被调用两次时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆