find the sum of the multiples of 3 and 5 below 1000

前端 未结 12 961
我在风中等你
我在风中等你 2021-01-18 08:04

Ok guys, so I\'m doing the Project Euler challenges and I can\'t believe I\'m stuck on the first challenge. I really can\'t see why I\'m getting the wrong answer despite my

12条回答
  •  孤街浪徒
    2021-01-18 08:56

    Okay, so this isn't the best looking code, but it get's the job done.

    public class Multiples {
    
    public static void main(String[]args) {
        int firstNumber = 3;
        int secondNumber = 5;
        ArrayList numberToCheck = new ArrayList();
        ArrayList multiples = new ArrayList();
        int sumOfMultiples = 0;
        for (int i = 0; i < 1000; i++) {
           numberToCheck.add(i);
    
           if (numberToCheck.get(i) % firstNumber == 0 || numberToCheck.get(i) % secondNumber == 0) {
               multiples.add(numberToCheck.get(i));
           }
    
        }
    
        for (int i=0; i

提交回复
热议问题