递归算法,运算1-2+3-4+5-6+...+n

来源:互联网 发布:零复网络是诈骗吗 编辑:程序博客网 时间:2024/05/29 18:02
public class Test1 {
public static void main(String args[]){
int n = 10;
int rst = getResult(n);
System.out.println(n+"的执行结果:"+rst);
}


public static int getResult(int n){
int result = 0;
for(int i=1;i<=n;i++){
switch(i%2){
case 0:result -=i;
break;
case 1: result += i;
break;
}
}
return result;
}
}
原创粉丝点击