我對遞歸的想法很新,這實際上是我第一次嘗試寫遞歸方法。重構這個遞歸方法?
我試圖實現一個遞歸函數Max,它傳遞一個數組以及一個保存數組大小以便打印最大元素的變量。
它的作品,但它只是沒有感覺正確!
我也注意到,我似乎使用static修飾符比我一般同學更多...
任何人都可以請提供任何一般提示,以及反饋,我怎麼能提高我的代碼?
public class RecursiveTry{
static int[] n = new int[] {1,2,4,3,3,32,100};
static int current = 0;
static int maxValue = 0;
static int SIZE = n.length;
public static void main(String[] args){
System.out.println(Max(n, SIZE));
}
public static int Max(int[] n, int SIZE) {
if(current <= SIZE - 1){
if (maxValue <= n[current]) {
maxValue = n[current];
current++;
Max(n, SIZE);
}
else {
current++;
Max(n, SIZE);
}
}
return maxValue;
}
}
喜歡標記「不做作業」。我不介意作業,只要你給它一個好的鏡頭,就像上面那樣...... :) – TheSoftwareJedi 2008-10-29 01:15:19