-8
我明白这个方法所做的一切,直到m=1
和n=1
。 if()
之后当m=1
和n=1
有问题时会发生什么情况。为什么这个递归方法被认为是一个阶乘?
public class exc {
public static void main(String[] args) {
System.out.println(prod(1, 4));
}
public static int prod(int m, int n) {
if (m == n) {
return n;
} else {
int recurse = prod(m, n - 1);
int result = n * recurse; // how n gets its value here?
return result;
}
}
}
什么是递归的价值?我的意思是如何产生只有一个 整数? (因为它有两个整数)
如果当m = 1且n = 1时返回值为1,那么为什么程序不会在那里终止?而是在“返回结果”之后终止。在其他?
在m = 1且n = 1之后,n是2(在递归中); n如何设置为2?是否因为2还在记忆中,需要处理?
我检查this page
我也用不同的println行之后看出来说,但没有帮助。我也使用了一个调试器。
如何在调试中运行此操作并查看? – Guy
它被认为是一个阶乘因为它*是*。不清楚你在问什么。 – EJP
[Understanding recursion]可能的重复(http://stackoverflow.com/questions/717725/understanding-recursion)和http://stackoverflow.com/questions/3021/what-is-recursion-and-when-should- i-use-it – Azodious