Project Euler #1 in Java

前端 未结 8 1625
旧时难觅i
旧时难觅i 2021-02-04 18:22

I\'m having problems with this code. I don\'t want to look at others, so I\'m wondering what\'s wrong with mine.

If we list all the natural numbers below 10 that are mu

8条回答
  •  囚心锁ツ
    2021-02-04 18:59

    You should do:

    for (int i = 0; i < 1000; i++) {
        if (i % 3 == 0 || i % 5 ==0) {
            temp += i;
        }
    }
    

    This will add each number only one time. In your code, you will add 15 twice, since it'll be satisfied in both conditions in both loops.

    Also note that according to the requirements, you should loop to < 1000, not <=.

提交回复
热议问题