我的方法是,例如,如果int [] num123 = {1,2,3};应该输出123。在主要方法。它正在输出零点。在convert num方法中,当我更改最后一个零时,它只是输出它被替换的任何数字。我们不允许使用任何循环,所以这就是我难住的。递归方法问题
public int convertNum(int[] num) {
return numHelper(num, 0, num.length - 1, 0);
}
private int numHelper(int[] num, int atIndex, int lastIndex, int result) {
atIndex = num.length - 1;
if (atIndex == lastIndex) {
return result;
}
if (num.length > 0) {
atIndex += 1;
}
return (int) (num[atIndex] * Math.pow(10, lastIndex - atIndex))
+ numHelper(num, atIndex + 1, lastIndex, result);
}
传递参数'atIndex'的意义是什么,当你做的第一件事是用'atIndex = num.length - 1'代替时,**立即结束递归**? – Andreas
固定。现在它是outoutput 20 –
请澄清您的具体问题或添加额外的细节,以确切地突出你所需要的。正如目前所写,很难确切地说出你在问什么。 –