maxRec()
意思是使用帮助程序 方法maximize()
计算数组内的最大值。当这个代码执行时,它似乎总是返回零,但是 它将打印出正确的值。在使用调试器时,我注意到 的maxRec()
方法会得到正确的返回值,但不会返回它;相反,它将其设置回零,并移至else语句。我将非常感谢任何可以帮助解决此问题的建议。找到最大值的递归方法
public int maxRec(int[] v) {
int maxValue = 0;
int[] tempArray = maximize(v);
boolean executeCode = true;
if (tempArray.length == 1) {
maxValue = tempArray[0];
executeCode = false;
System.out.println(maxValue);
} else if (executeCode == true && tempArray.length != 1) {
maxRec(tempArray);
}
return maxValue;
}
public int[] maximize(int[] v) {
int count = 0;
int secondCount = 0;
for (int i = 0; i < v.length; i++) {
if (v[i] > v[0]) {
count++;
}
}
int[] newArray;
newArray = new int[count];
for (int i = 0; i < v.length; i++) {
if (v[i] > v[0]) {
newArray[secondCount] = v[i];
secondCount++;
}
}
return newArray;
}
你最好忘记你的方法,并看看这个问题:http://stackoverflow.com/a/19590391/2001247。似乎更友好...... – ElDuderino 2014-10-09 16:05:05