java超出了的循环中的范围
此代码正在运行,但很难看:java超出了的循环中的范围,java,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(inti=0,l=1;i
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(inti=0,l=1;i
我想把他换成这样:
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;
}
for(inti=0;i
此操作返回错误:字符串索引超出范围
。我理解原因:当涉及到最后一个字母时,“char nextRank=word.charAt(I+1);”已无事可做
但我不知道如何解决这个问题 患者:“在那种情况下做这件事很痛”
医生:“那么,如果符合条件,就不要这样做。”
if(i+1
从1开始i
for (int i = 1; i < word.length(); i++) {
char c = word.charAt(i-1);
j = (int) c;
char nextRank = word.charAt(i);
k = (int) nextRank;
}
for(inti=1;i
int length=word.length()-1;
for(int i=0;i
只需将i的限制降低1
for (int i = 0; i < word.length()-1; i++) {
char c = word.charAt(i);
j = (int) c;
char nextRank = word.charAt(i+1);
k = (int) nextRank;
}
for(inti=0;i
只需从0循环到word.length()-1即可
for(inti=0;i
怎么样
for (int i = 0; i < word.length() - 1; i++) {
for(inti=0;i
如果你不知道,那么谁知道呢?
for (int i = 0; i < word.length()-1; i++) {
char c = word.charAt(i);
j = (int) c;
char nextRank = word.charAt(i+1);
k = (int) nextRank;
}
for (int i = 0; i < word.length() - 1; i++) {
char c = word.charAt(i);
j = (int) c;
char nextRank = word.charAt(i+1);
k = (int) nextRank;
}
for (int i = 0; i < word.length() - 1; i++) {