1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public class RecursionTest {
public static void main(String[] args) {
int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println(sum); RecursionTest test = new RecursionTest(); int sum1 = test.getSum(100); System.out.println(sum1); System.out.println("*****************"); int value = test.f(10); System.out.println(value);
}
public int getSum(int n) {
if (n == 1) { return 1; } else { return n + getSum(n - 1); }
}
public int getSum1(int n) {
if (n == 1) { return 1; } else { return n * getSum1(n - 1); }
} public int f(int n){ if(n == 0){ return 1; }else if(n == 1){ return 4; }else{
return 2*f(n - 1) + f(n - 2); } }
}
|