-
尧叔
循环写法大家都会,我再补充两个//递归
public int cool(int a) {
if (a < 0||a % 2 == 0) {
throw new RuntimeException("请输入大于1的基数");
}
if (a == 1) {
return 1;
}
return a + cool(a - 2);
}
//数学归纳法
public int cool2(int a) {
if (a < 0||a % 2 == 0) {
throw new RuntimeException("请输入大于1的基数");
}
return a * a / 4 + a / 2 + 1;
}
-
yanrun
public int doCalculate() {
int sum = 0;
int i = 1;
while(i <= 99) {
sum = sum + i;
i = i + 2;
}
return sum;
}
-
田心枫
public static void main(String[] args) {
int sum =0;
for (int i=0;i<50;i++){
sum = sum + (2*i)+1;
}
System.out.println(sum);
}