Java程序将编译,但不会在命令提示符下运行 公共类ProjectOne { 公共静态void main(字符串[]args) { int i,count1=0,count2=0,count3=0; int sum1=0,sum2=0,sum3=0,总计; 对于(i=1;i
您正在重置Java程序将编译,但不会在命令提示符下运行 公共类ProjectOne { 公共静态void main(字符串[]args) { int i,count1=0,count2=0,count3=0; int sum1=0,sum2=0,sum3=0,总计; 对于(i=1;i,java,windows,command-prompt,Java,Windows,Command Prompt,您正在重置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) //cre
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 (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
}
}
公共类ProjectOne
{
公共静态void main(字符串[]args)
{
int i,count1=0,count2=0,count3=0;
int sum1=0,sum2=0,sum3=0,总计;
对于(i=1;i您没有遗漏任何关于命令提示符的内容。您已经创建了一个无限循环-您的程序正在一次又一次地做同样的事情
回想一下,在Java(和C,以及许多使用C派生语法的语言)中,类似这样的for
循环:
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
}
}
请注意,我已将I
更改为j
(并在每个循环中声明了j
;这样每个循环都会得到一个名为j
的单独变量,但如果需要,您可以在main
的开头声明一次,并且不会有任何区别)
您的程序仍然无法正常工作(它将给您错误的答案),但这将修复您所问的无限循环。您是如何从命令提示符编译和运行此程序的?哎呀。您正在同时修改循环中的i
。在每次循环迭代结束时,i=count3+1
。是的@Elliott Frisch您写的,这就是我提供正确和修改答案的原因.我把内部循环中的i改为j,使循环无限大,现在循环不再处于无限状态。上面的程序将把这个77777614作为输出返回。
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;
}
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