我创建了仅在第一次使用此循环,然后我收到一个数组索引超出界限异常。
for(int a=0; a<pkcode.length;a++){
for(int b=0;b<trainercode.length;a++){
if(pkcode[a]==trainercode[b]){
w=a+1;
v=b+1;
System.out.println("Your egg(s) that match with trainers are:");
System.out.println("egg #" +w+ ": " + pkcode[a] + " matches with trainer #" +v+ ": " + trainercode[b]);
}
}
}
有人知道怎么修这个东西吗?
最佳答案
for(int b=0;b<trainercode.length;a++){
应该
for(int b=0;b<trainercode.length;b++){
您正在增加错误的值(内部循环使用
b
,但增加了a
)。这将使您在内部循环中具有无限循环(
b
永远不会增加,因此永远不会达到结束条件),它将以两倍的速度达到您的a
并超出pkcode[a]
的范围。