您的位置:首页 > 其它

题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。

2014-07-02 10:44 736 查看
比如要计算1到100的累加,代码如下,主要还是应用递归思想:

public class Main{
static int sum=0;
boolean theMethod(int n){
sum+=n;
return (n-1<=0||theMethod(n-1)); //核心代码,若n-1小于0,则递归结束
}
public static void main(String[] args){
new Main().theMethod(100);
System.out.println(Main.sum);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐