此代碼工作,但醜:的java超出範圍環路
for (int i = 0, l=1; i < word.length() && l < word.length(); i++, l++) {
char c = word.charAt(i);
j = (int) c;
char nextRank = word.charAt(l);
k = (int) nextRank;
}
我想他變成這樣的事情:
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
j = (int) c;
char nextRank = word.charAt(i+1);
k = (int) nextRank;
}
這個方法返回一個錯誤:String index out of range
。我明白爲什麼:當涉及到最後一個字母「char nextRank = word.charAt(i + 1);」已經無事可做了。
但我不知道如何解決這個問題!
如果你不那麼知道誰知道? –