package com.clean.array;

import java.util.Scanner;

/**
 * 递归求三角数字
 */
public class TranTest {
    public static void main(String[] args) {
        TranTest tranTest = new TranTest();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("please input a Integer value:");
            String x = scanner.next();
            if(x.equalsIgnoreCase("q")) {
                System.out.println("quit!");
                break;
            }
            int y = Integer.parseInt(x);
//            int v = tranTest.triange(y);
            int v = tranTest.triangeDg(y);
            System.out.println("res = " + v);
        }
    }


    private int triangeDg(int n) {
        System.out.println("enter : " + n);
        if(n == 1) {
            System.out.println("retrun 1");
            return 1;
        } else {
            int temp = n + triangeDg(n - 1);
            System.out.println("return :" + temp);
            return temp;
        }
    }

    /**
     * 循环求解三角数字
     * @param n
     * @return
     */
    private int triange(int n) {
        int total = 0;
        while (n > 0) {
            total = total + n;
            --n;
        }
        return total;
    }
}
10-08 03:24