2 回答
TA贡献1829条经验 获得超9个赞
此更改应该解决此问题。
vowels(input, currentPos, amount);
到
amount = vowels(input, currentPos, amount);
TA贡献1842条经验 获得超12个赞
提高速度并更好地使用递归方法。
所有输入参数甚至可以是只读的。
public static int vowels(final String input, final int currentPos, final int amount) {
// Create the arrays of chars to hold vowels and the input
System.out.println("Amount at beginning: " + amount);
char[] vow = {'a', 'e', 'i', 'o', 'u'};
char[] entered = input.toLowerCase().toCharArray();
int stringLength = input.length();
System.out.println("stringlength " + stringLength);
if (currentPos < stringLength) {
for (int i=0; i<5; i++) {
if (entered[currentPos] == vow[i]) {
System.out.println("vowel: " + entered[currentPos]);
System.out.println("amount: " + amount);
return vowels(input, currentPos + 1, amount + 1);
}
}
System.out.println("Amount before calling the recursive function: " + amount);
System.out.println("currentPos before calling the recursive function: " + currentPos);
return vowels(input, currentPos + 1, amount);
}
System.out.println("Amount before returning the final amount: " + amount);
return amount;
}
添加回答
举报