为什么这两个代码的输出不同?
func 1:按预期反向打印系列
public static void printSeriesInReverse(int n){
if (n > 0){
System.out.println(n);
printSeriesInReverse(n-1);
}
func 2:打印系列正常而不反转它。
public static void printSeriesInReverse(int n){
if (n > 0){
printSeriesInReverse(n-1);
System.out.println(n);
}
为什么打印语句或函数调用首先出现如此显着的差异?
答案 0 :(得分:0)
因为它是一个递归函数。我们假设你用Traceback (most recent call last):
File "C:/Users/helpmecoding/PycharmProjects/untitled/distance.py", line 31, in
<module>
distances = gmaps.distance_matrix((GSGS), (schools), mode="driving")['rows'][0]['elements'][0]['distance']['text']
KeyError: 'distance'
...
对于第一种情况,你会得到:
n=3
最终输出:printSeriesInReverse(3)
3 > 0 // YES
prints 3
printSeriesInReverse(2)
2 > 0 // YES
prints 2
printSeriesInReverse(1)
1 > 0 // YES
prints 1
printSeriesInReverse(0)
0 > 0 // NO
// End of recursion
对于第二种情况,你会得到:
321
最终输出:printSeriesInReverse(3)
3 > 0 // YES
printSeriesInReverse(2)
2 > 0 // YES
printSeriesInReverse(1)
1 > 0 // YES
printSeriesInReverse(0)
0 > 0 // NO
// End of recursion
prints 1
prints 2
prints 3