0

Is it illegal to set j = i in the second for loop? It seems to work for the first iteration, however, after that it doesnt print anything, is there a more appropriate way to do this? I rewrote it to use a while loop, and it worked perfectly, so whats wrong with the for loop?

public class DaysOfChristmas {
public static void main ( String[] args )
{
    int i,j;
    String day = "";
    String verse = "";

    for ( i = 1; i <= 12; i++)
    {
        switch (i)
        {
            case 1: day = "first";
            break;

            case 2: day = "second";
            break;

            case 3: day = "third";
            break;

            case 4: day = "fourth";
            break;

            case 5: day = "fith";
            break;

            case 6: day = "sixth";
            break;

            case 7: day = "seventh";
            break;

            case 8: day = "eight";
            break;

            case 9: day = "ninth";
            break;

            case 10: day = "tenth";
            break;

            case 11: day = "eleventh";
            break;

            case 12: day = "twelfth";
            break;
        }

        System.out.printf("On the %s day of Christmas my true love gave to me\n", day);

        for ( j = i; j == 1; j--)
        {
            switch (j)
            {
                case 1: verse = "A Partridge in a Pair Tree";
                break;

                case 2: verse = "Two Turtle Doves";
                break;

                case 3: verse = "Three French Hens";
                break;

                case 4: verse = "Four Colly Birds";
                break;

                case 5: verse = "Five Golden Rings";
                break;

                case 6: verse = "Six Geese-a-Laying";
                break;

                case 7: verse = "Seven Swans-a-Swimming";
                break;

                case 8: verse = "Eight Maids-a-Milking";
                break;

                case 9: verse = "Nine Ladies Dancing";
                break;

                case 10: verse = "Ten Lords-a-Leaping";
                break;

                case 11: verse = "Eleven Pipers Piping";
                break;

                case 12: verse = "Twelve Drummers Drumming";
                break;
            }
            System.out.printf("%s ", verse);
        }
        System.out.println();

    }
}

}

3
  • 1
    Illegal: No! You will never be fined for that! But you might want to consider the logic in your code :) Commented Sep 30, 2012 at 2:50
  • what are you trying to do, can you explain Commented Sep 30, 2012 at 2:56
  • The comment by @SidMS is correct. In addition, the code would be significantly shorter, easier to read, and less at risk for typos if you used arrays of strings instead of the switch statements. Commented Sep 30, 2012 at 3:07

2 Answers 2

4
for ( j = i; j >= 1; j--)

should work. The problem was that the second loop ONLY EXECUTES when j is 1. Otherwise it doesn't execute at all. And j is 1 only the first time when i is 1.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for detecting the bug in his code. However it is always a good idea to explain what was wrong with the code before posting a solution to the answer :)
I'll keep that in mind when answering questions in future. Thanks!
0

It's not illegal to do that. However, consider your condition in the loop:

 for ( j = i; j == 1; j--)

That loop will only ever execute when j is exactly 1. You should definitely rethink your logic here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.