![](/img/trans.png)
[英]Java Program compile and run in command prompt successfully but not in myeclips?
[英]Java program will compile but not run in command prompt
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项目,尝试从事一些编码技能和数学方面的工作(我相对较新,正在为一门课程学习Java)。 无论哪种方式,我都会创建这段代码,该代码段应将小于1000的3和5的所有倍数相加。我去编译该代码,并且编译得很好,但是当我运行它时,命令提示符(顺便说一句,我正在运行Windows 8.1)坐在那里什么也不做,什么也不响应,坐在那里的光标闪烁。 我习惯于使用Python的IDLE进行编程,因此几乎没有关于命令提示符的练习。 我是不是很急躁,还是有些功能无法正常运行?
您正在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
}
}
我希望这将是您的解决方案。
您不会丢失有关命令提示符的任何信息。 您已经创建了一个无限循环-您的程序一次又一次地执行相同的操作。
回想一下在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
,它将i加1。 因此,在循环结束时(恰好在}
之前, i
等于count3 + 2
(即count3 + 1 + 1
)。
count3
是您的程序到目前为止遇到的15的倍数,因此开始时将为0。 因此,这将在循环结束之前有效地将i
重置为2,并且i
永远不会超过2。
您可能打算在内部for
循环中使用其他变量:
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
的开头声明一次,这没有什么区别)。
您的程序仍将无法正常运行(它将给您错误的答案),但这将解决您所要求的无限循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.