public class ProjectOne { public static void main (String[] args) { int i, count1 = 0, count2 = 0, count3 = 0; int sum1 = 0, sum2 = 0, sum3 = 0, total; for(i=1; i<1000; ++i) //creates loop that will iterate for every number { if (i%3 == 0) count1 += 1; //gathers total #'s <1000 that can be divided by 3 if (i%5 == 0) count2 += 1; //same as above, but divisible by 5 if (i%3 == 0 && i%5 ==0) count3 += 1; //gathers count for where sets intersect for (i=1; i<=count1; ++i) sum1 += 3*i; //creates sum for all multiples of 3 for (i=1; i<=count2; ++i) sum2 += 5*i; //creates sum for all multiples of 5 for (i=1; i<= count3; ++i) sum3 += 15*i; //creates sum for where sets intersect } total = (sum1 + sum2) - sum3; //totals two sums while subtracting //the intersections that would double System.out.print (total); // prints total value } }
好的,我正在通过欧拉计划(Euler)尝试编写一些编程技巧和math(我相对较新,正在为一个class学习Java)。 无论哪种方式,我创build了这段代码应该是3和5的所有倍数小于1000.我去编译代码,它编译得很好,但是当我去运行它的命令提示符(我正在运行Windows 8.1)坐在那里什么都不做,什么都没有响应,光标只是坐在那里闪烁。 我习惯使用Python的IDLE进行编程,所以在命令提示符下做了一些练习。 我只是感到不耐烦,还是有些东西不能正常运转?
您不会错过有关命令提示符的任何信息。 你创建了一个无限循环 – 你的程序一遍又一遍地做同样的事情。
回想一下,在Java(以及C语言和C语言衍生的语言)中,像这样的for
循环:
for (i=1; i<= count3; ++i) sum3 += 15*i; //creates sum for where sets intersect
做同样的事情:
i=1; while(i <= count3) { sum3 += 15*i; //creates sum for where sets intersect ++i; }
这意味着你的代码是这样的:(我也改变了格式略有删除评论为简洁起见)
i=1; while(i<1000) { if (i%3 == 0) count1 += 1; if (i%5 == 0) count2 += 1; if (i%3 == 0 && i%5 ==0) count3 += 1; i=1; while(i <= count1) { sum1 += 3*i; ++i; } i=1; while(i <= count2) { sum1 += 5*i; ++i; } i=1; while(i <= count3) { sum1 += 15*i; ++i; } // HERE ++i; }
请注意,每当程序到达我标记为“HERE”的行时, i
将等于count3 + 1
(因为如果它小于或等于count3
那么它仍然会在“HERE”之前的循环中)。
下一条指令是++i
,它给我增加了1。 所以在循环结束时(就在}
之前, i
将等于count3 + 2
(即count3 + 1 + 1
)。
count3
是你的程序到目前为止所遇到的15的倍数,所以在开始的时候是0。 所以在循环结束之前, i
有效地将i
重置为2,并且i
永远不会超过2。
你可能打算在你的内循环中使用一个不同的变量:
for (int j=1; j<=count1; ++j) sum1 += 3*j; //creates sum for all multiples of 3 for (int j=1; j<=count2; ++j) sum2 += 5*j; //creates sum for all multiples of 5 for (int j=1; j<= count3; ++j) sum3 += 15*j; //creates sum for where sets intersect
请注意,我已经将i
更改为j
(并且在每个循环中声明了j
;这样每个循环都会获得一个名为j
的单独变量,但是如果需要,您可以在main
的开始处声明一次,这样做没有任何区别)。
你的程序仍然不能正常工作(它会给你一个错误的答案),但是这将解决你所问的无限循环。
你正在重置你的for
循环的i
变量,导致它永远不会结束。 试试这个修改后的代码
public class ProjectOne { public static void main (String[] args) { int i, count1 = 0, count2 = 0, count3 = 0; int sum1 = 0, sum2 = 0, sum3 = 0, total; for(i=1; i<1000; ++i) //creates loop that will iterate for every number { if (i%3 == 0) count1 += 1; //gathers total #'s <1000 that can be divided by 3 if (i%5 == 0) count2 += 1; //same as above, but divisible by 5 if (i%3 == 0 && i%5 ==0) count3 += 1; //gathers count for where sets intersect for (int j=1; j<=count1; ++j) sum1 += 3*j; //creates sum for all multiples of 3 for (int j=1; j<=count2; ++j) sum2 += 5*j; //creates sum for all multiples of 5 for (int j=1; j<= count3; ++j) sum3 += 15*j; //creates sum for where sets intersect } total = (sum1 + sum2) - sum3; //totals two sums while subtracting //the intersections that would double System.out.print (total); // prints total value } }
我希望这将是你的解决方案。