Java程序将编译但不在命令提示符下运行

时间:2015-01-06 05:07:31

标签: java windows 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
    }
}

好的,所以我正在通过Project Euler尝试编写一些编码技巧和数学(我相对较新并且正在为一门课学习Java)。无论哪种方式,我创建这段代码,应该总结3和5的所有小于1000的倍数。我去编译代码,它编译得很好,但当我去运行它命令提示符(我顺便运行Windows 8.1)在那里什么都不做,什么都没做,光标只是坐在那里闪烁。我以前习惯使用Python的IDLE进行编程,所以在命令提示符下几乎没有练习。我只是感到不耐烦,或者有什么东西不能正常运作吗?

2 个答案:

答案 0 :(得分:1)

您正在重置i循环中的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
    }
}

我希望这将是您的解决方案。

答案 1 :(得分:1)

您没有遗漏任何有关命令提示符的内容。你已经创建了一个无限循环 - 你的程序一遍又一遍地做同样的事情。

回想一下,在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;
}

请注意,每次程序到达我标记为#34; HERE&#34;的行时,i将等于count3 + 1(因为如果它小于或等于{{ 1}}然后它仍然会在#34; HERE&#34;)之前的循环中。

下一条指令是count3,只是将i加1。因此,在循环结束时(++i之前,}将等于i(即count3 + 2)。

count3 + 1 + 1是程序到目前为止遇到的15的倍数,因此在开始时它将为0。因此,这有效地在循环结束前将count3重置为2,并且i永远不会超过2。

您可能打算在内部i循环中使用不同的变量:

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的开头,如果你想要它并没有任何区别。)

你的程序仍然无法正常工作(它会给你一个错误的答案),但这将解决你所询问的无限循环。