仍在学习,我似乎无法总结我的脑袋上似乎像一件容易的事。 的computeMethods方法的就是IM完全以难倒,但是相反的方法,我只是不断取回相同的整数而不会被逆转。编程作业递归
/****************************
* For Method Computemethods1 i must compute series
* b(x)=1/3+2/5+3/7..... +x/2x+1
* For method ComputeMethod2
* 1/2+2/3+......... x/(x+1)
*******************************/
public static int computeMethod1(int x){
if (x==0)
return 0;
if (x==1)
return 1;
return computeMethod1(x-1/3/(x-1))+computeMethod1(x-2/3/(x-2));
}
public static int computeMethod2(int x){
if (x==0)
return 0;
return computeMethod2((x-1)/(x-1)+1)+computeMethod2((x-2)/(x-2)+1);
}
/********************
* For method reverseMethod i must reverse a user given int
**********************/
public static int reverseMethod(int x){
int reversedNum=0;
if (x!=0)
return x;
reversedNum=reversedNum *10 +x%10;
return reversedNum+reverseMethod(x/10);
}
/******************
* For method sumDigits i must use recursion
* to sum up each individual number within the int
********************/
public static long sumDigits(long n){
if(n==0)
return 0;
if (n==1)
return 1;
else
return n+sumDigits(n-1);
}
}
'X-1/3'可能与整数除法的问题?尝试使用'float'或'double'数字,即'x-1。/ 3.'并相应地更改方法的参数。另外,sumDigits似乎有点偏离...实际上,它更像是“从1到n的所有数字的总和”。 – 2015-02-09 09:29:47
你的问题是什么?您应该指定什么不按预期工作。另外,请删除不相关的代码。 – 2015-02-09 09:30:39