选举程序中的java ArrayIndexOutOfBoundsException
我有一小段代码,每当我运行它时都会抛出一个异常,我不明白为什么:
public class Debug
{
public static void main (String[] args)
{
String[] electionName = {"John Smith", "Mary Miller", "Michael Duffy", "Tim Robison", "Joe Ashtony"};
int[] electionVotes = {5000, 4000, 6000, 2500, 1800};
int i = 0;
for (i = 0; i < electionVotes.length; i++);
{
System.out.println(electionName[i] + electionVotes[i]);
}
}
}
有人能帮我吗
# 1 楼答案
失去信心;在你的“for”循环结束时
# 2 楼答案
for循环有一个小错误。该协议被终止
对于(i=0;i<;electionvows.length;i++)
所以for循环实际上什么都不做,只是将i增加到5,然后访问2个数组中索引5处的元素,这将抛出您看到的异常
# 3 楼答案
请看
for
语句的结尾。问题来自字符;
这相当于: