package prac;
public class Sum {
public static int sum(int [] arr){
return sum(arr, 0);
}
public static int sum(int[]arr,int index){
if (index==arr.length)
return 0;
return arr[index]+sum(arr,index+1);
}
public static void main(String[] args) {
int[]arr={1,2,3,4,5,6,7,8};
int sum = sum(arr);
System.out.println(sum);
}
}
递归数组求和
最新推荐文章于 2024-07-05 12:20:47 发布