我的 java 代码中出现以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
...出现在以下行:
if (strFactor.charAt(j) == strProduct.charAt(k)) {
这是代码:
for (int j=0; 0<=5; j++) {
same = false;
for (int k=0; k<=5; k++) {
if (strFactor.length() == 6 && strProduct.length() == 6) {
if (strFactor.charAt(j) == strProduct.charAt(k)) {
same = true;
}
}
}
if (!same) {
return false;
}
}
有人知道为什么索引会超出范围吗?
最佳答案
我不是 Java 程序员,但是 0<=5
将始终评估为 true
,所以j
达到6
(如果没有那个异常(exception),我们会继续下去)。
关于java:字符串索引超出范围:6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8428132/