I'm pretty sure it's because you don't terminate your recursion properly. In order to check the n+1
character, the current n has to be n-2
or the value of n+1
is going to be out of range. That's because charAt()
is limited to at most n-1
.
So you want to make sure that n
is less than n-1
before you pass it.
else if (n < userWord.length()-1) {
elfish(userWord, n + 1);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…