当我在网上找到一个挑战,让程序员找到 1000 以下的所有 3 和 5 的倍数之和时,我遇到了一个问题。这是我的代码:
public class Challenge {
public static int i = 0;
public static int counter = 0;
public static int sum = 0;
public static int[] multiples3;
public static int[] multiples5;
public static void main(String args[]) throws Exception {
multiples3 = new int[1000];
multiples5 = new int[1000];
while(counter < 1000) {
multiples3[counter] = 3*counter;
multiples5[counter] = 5*counter;
System.out.println("Adding: " + sum + ", " + multiples5[counter] + ", and " + multiples3[counter]);
sum = sum + (multiples5[counter] + multiples3[counter]);
counter++;
System.out.println("Progressive Sum: " + sum + "\n" + "____________");
if(multiples5[counter] >= 1000) {
System.out.println("++++ FIVES DONE ++++");
while(multiples3[counter] <= 999) {
sum = (sum + multiples3[counter]);
counter++;
System.out.println("Progressive Sum: " + sum + "\n" + "____________");
if(multiples3[counter] >= 999) {
System.out.println("~~~~~~ DONE ~~~~\n Final result: " + sum);
}
}
}
}
}
}
我在这一行得到一个错误:if(multiples5[counter] >= 1000) {
我假设它与我尝试使用数组有关,但我不完全确定,因为我是数组新手。另外,我是否以正确的方式应对这一挑战?我在这里先向您的帮助表示感谢!