即时添加0和1000之间的所有数字,是倍数或3和5.我只是无法添加它们。我不断收到错误消息:异常线程 “main” java.lang.IndexOutOfBoundsException:指数:468,大小:468尝试添加arraylist
我的代码
//Multiple of 3 and 5 up to 1000
int total = 0;
int answer = 0;
ArrayList<Integer> multof3 = new ArrayList<Integer>();
for(int i =0; i <=1000; i++){
if(i % 3 == 0 || i % 5==0){
multof3.add(i);
total++;
}else{
continue;
}
}
System.out.println(multof3);
System.out.println(total);
//looping through array to get sum of elements
for(int x =0; x <= multof3.size(); x++){
answer= answer + multof3.get(x);
}
System.out.println(answer);
任何人都知道的原因?我不明白为什么它不工作。它打印出的ArrayList所以我肯定要补充的元素结合在一起......
'X <= multof3.size()' - >'X
assylias
信息您不需要使用“继续;”当你已经结束循环 – Mani