计算1加到100的和:
package com.jike.exception;public class digui { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(sum(100)); } public static int sum(int i) { if (i==1) { return 1; }else { return i+sum(i-1); } }}
输出:
5050