public class Test {
public static void main(String[] args) {
System.out.println(getSum(3));
}
public static int getSum(int number) {
int sum = 0;
for (int i = 1; i <= number + 1; i++) {
for (int j = 0; j < i; j++) {
sum += j;
}
}
return sum;
}
}
java实现一个方法,参数为n,返回1+(1+2)+ … +(1+2+…+n)的值;
最新推荐文章于 2024-03-04 22:53:52 发布