仍在學習,我似乎無法總結我的腦袋上似乎像一件容易的事。 的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