简体   繁体   English

欧拉计画1是否无法打印出所有数字?

[英]Project Euler 1 does not print out all digits?

Find the sum of all the multiples of 3 or 5 below 1000. 找出1000以下3或5的所有倍数的总和。

My program works for siphoning out all the multiples of 3 and 5, but when I run it to 1000 it only prints out the multiples of 3 and 5 from 363 onwards. 我的程序适用于虹吸3和5的所有倍数,但是当我将其运行到1000时,它仅从363起打印出3和5的倍数。

My question is, why? 我的问题是,为什么? This is my code: 这是我的代码:

class threeAndFive {
    public static void main(String[] args) {
        for(int i = 1; i < 1000; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
            } else if (i % 5 == 0) {
                System.out.println(i);
            } else if (i % 3 == 0 && i % 5 == 0) {
                System.out.println(i);
            }
        }
    }
}

Sample of output: 输出样本:

The first 7 lines of output are 输出的前7行是

363
365
366
369
370
372
375

The last 7 lines of output are 输出的最后7行是

985
987
990
993
995
996
999

So how come, if judging by my for loop it should iterate from 3 right up to 999, it is starting output at 363? 那么,如果通过我的for循环判断,它应该从3迭代到999,那么它将开始输出363?

EDIT 编辑

I found that I was meant to be finding the SUM, not printing out the numbers. 我发现我是要查找SUM,而不是打印出数字。 Either way, my question still stands. 无论哪种方式,我的问题仍然存在。

The code is working perfectly. 该代码运行正常。 Your IDE (presumably Eclipse) is simply running out of display room, and won't show you the previous lines. 您的IDE(大概是Eclipse)仅在显示室中用完了,不会向您显示前几行。 It is possible to configure the number of lines that will be stored in the output console. 可以配置将存储在输出控制台中的行数。 You could also print the numbers out with just spaces between them and get around the limitation that way. 您也可以将数字打印出来,并在它们之间仅留空格,以这种方式解决限制。

The order of if statements is also wrong in terms of logic. 就逻辑而言,if语句的顺序也是错误的。

if (i % 3 == 0 && i % 5 == 0) 
{
       System.out.println(i);
}
else if (i % 3 == 0) 
{
       System.out.println(i);
} 
else if (i % 5 == 0) 
{
    System.out.println(i);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM