ITeye博客

递归实现加法

alovejun14 · 2010-08-27 20:35 · ASP.NET · 编程语言 · Markdown

  1. public class SumPlus
  2. {
  3. public static void main(String args[])
  4. {
  5. int[] num = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  6. int x = 10;
  7. SumPlus test = new SumPlus();
  8. System.out.println(test.sum(num, x));
  9. }
  10. public int sum(int[] num, int n)
  11. {
  12. if (n > 0)
  13. {
  14. return num[n - 1] + sum(num, n - 1);
  15. }
  16. else
  17. return 0;
  18. }
  19. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。