求1+2+3+···+··n,不能使用x和/,for,if,while,switch

求1+2+3+···+··n,不能使用x和/,for,if,while,switch

import java.util.Scanner;
public class Demo_65 {
    public static void main(String[] args) {
        int res = new Demo_65().getSum(new Scanner(System.in).nextInt());
        System.out.println(res);
    }

    private int getSum(int n) {
        return n > 0 ? n + getSum(n-1) : 0;
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章