【发布时间】:2010-09-19 17:10:18
【问题描述】:
我对递归的想法很陌生,这实际上是我第一次尝试编写递归方法。
我尝试实现一个递归函数 Max,它传递一个数组,以及一个保存数组大小的变量,以便打印最大的元素。
它有效,但它感觉不对!
我还注意到,我似乎比我的同学们使用静态修饰符的次数要多得多……
任何人都可以就如何改进我的代码提供任何一般性提示和反馈吗?
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;
}
}
【问题讨论】:
-
喜欢“不做作业”的标签。我不介意家庭作业,只要你给它一个好机会,就像上面一样...... :)