用java实现了1-100的累加 private static long count=1;
private static long add(long num){
long t=num;
if(count>=1 && count<=100){
t=t+count;
count++;
add(t);
}else{
return t;
}
return t;
}
//调用
System.out.println(add(1));//打印的值是2...没明白

解决方案 »

  1.   


     private static long count=1;
        private static long add(long num){
            long t=num;
            if(count>=1 && count<=100){
                t=t+count;
                count++;
                t=add(t);//没测试,但是你这里原来肯定不对,而且下面的那个else{return ...}可以去掉
            }
            return t;
        }
    //调用
    System.out.println(add(1));//打印的值是2...没明白
      

  2.   

     private static int n = 1;
     private static long sum = 0;
     public static long sumLong(int n){
     if(n>0&&n<101){
     sum+= n;
     n++;
     }
     else return sum;
     return sumLong(n);
     }
        //调用
        System.out.println(sumLong(n));