题解 | #牛牛学数列4#
牛牛学数列4
http://www.nowcoder.com/practice/586b114f52034688879342ba45779081
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Sum sum = new Sum();
System.out.println(sum.sumZs(n));
}
}
class Sum {
public int sumZs(int n) {
int sum = 0;
if(n > 0) {
for(int i = 1; i <= n; i++) {
sum += i;
}
return sum + sumZs(n - 1);
} else {
return 0;
}
}
}
Java基础练习题 文章被收录于专栏
都是一些基础的语法题目,每天可以刷几道。