简体   繁体   中英

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
    }
}

Okay so I'm running through Project Euler trying to work on some of coding skill and mathematics (I'm relatively new and am learning Java for a class). Either way, I create this piece of code that is supposed to sum all of the multiples of 3 and 5 that are less than 1000. I go to compile the code and it compiles just fine but when I go to run it the command prompt (I'm running Windows 8.1 by the way) sits there doing nothing and responding to nothing, the cursor just sitting there blinking. I'm used to programming using Python's IDLE and so have little practice with command prompt. Am I just being impatient or is there something that is not functioning quite the way it should?

You are resetting your i variable in your for loop, causing it to never end. try this modified code:

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 hope this will be your solution.

You are not missing anything about the command prompt. You have created an infinite loop - your program is doing the same thing over and over again.

Recall that in Java (and C, and many languages with C-derived syntax), a for loop like this one:

for (i=1; i<= count3; ++i)
    sum3 += 15*i; //creates sum for where sets intersect

does the same as this:

i=1;
while(i <= count3)
{
    sum3 += 15*i; //creates sum for where sets intersect
    ++i;
}

which means that your code is equivalent to this: (I've also changed the formatting slightly and removed the comments for brevity)

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;
}

Notice that each time the program gets to the line I marked "HERE", i will be equal to count3 + 1 (since if it was less than or equal to count3 then it would still be in the loop just before "HERE").

The next instruction is ++i , which just adds 1 to i. So at the end of the loop (just before the } , i will be equal to count3 + 2 (that is count3 + 1 + 1 ).

count3 is the number of multiples of 15 that your program has encountered so far, so that will be 0 at the start. So this effectively resets i to 2 just before the end of the loop, and i never gets past 2.

You probably meant to use a different variable in your inner for loops:

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

Notice that I have changed i to j (and declared j in each loop; this way each loop gets a separate variable called j , but you could declare it once at the start of main if you wanted and it would make no difference).

Your program will still not work properly (it will give you a wrong answer), but this will fix the infinite loop which is what you asked about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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