public class Example {
public static void main(String[] args) {
//方法的递归调用
int sum = getSum(4);
System.out.println("sum="+sum);
}
public static int getSum(int n) {
if (n==1){
return 1;
}
int temp = getSum(n-1);
return temp+n;
}
}
public class Example {
public static void main(String[] args) {
//方法的递归调用
int sum = getSum(4);
System.out.println("sum="+sum);
}
public static int getSum(int n) {
if (n==1){
return 1;
}
int temp = getSum(n-1);
return temp+n;
}
}
本文标题:Java |简单的递归
本文链接:https://www.haomeiwen.com/subject/ihugvltx.html
网友评论