been working on this for 2 days and can't figure out what I am doing wrong. We are suppose to input a day and the days gifts are to be in the output. When I put in my day it starts with the correct day but then loops through all twelve cases. I have break statement's but to no avail. Any help or ideas would be apimport

java.util.Scanner;//program uses scanner

public class ChristmasSong
{
    //shows user Christmas gifts based on day the user inputs.

    public static void main(String[] args) 
    {
        Scanner input = new Scanner ( System.in );
        int day; //used to select day to display gifts

        System.out.println ( "Enter the Day for the gifts you would like " +
                "to see (1-12)" );
        day = input.nextInt();
        if ( day < 1 )//check to see that a valid day is picked
        {
            System.out.println ( "Invalid Day: please try again- select between" +
                    "1 and 12" );
        }//end if

        else if ( day > 12)//check to see that a valid day is picked
        {
            System.out.println ( "Invalid Day: please try again- select between" +
                    "1 and 12");
        }//end else if

        System.out. print ( "One the " );

        while ( day >0 )
        {


            switch (day )//day based on user input
            {
                case 1:
                    System.out.print ( "first day of Christmas, \nmy true love " +
                            "gave to me\n A partidge in a pear tree\n\n\n" );
                    day--;
                    break;


                case 2:
                    System.out.print ( "second day of Christmas, \nmy true love" +
                            " gave to me\nTwo turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 3:
                    System.out.print ( "third day of Christmas, \nmy true love" +
                            " gave to me\nThree french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 4:
                    System.out.print ( "forth day of Christmas, \nmy true love" +
                            " gave to me\nFour calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 5:
                    System.out.print ( "fifth day of Christmas, \nmy true love" +
                            " gave to me\nFive golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 6:
                    System.out.print ( "sixth day of Christmas, \nmy true love" +
                            " gave to me\nSix geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 7:
                    System.out.print ( "seventh day of Christmas, \nmy true love" +
                            " gave to me\nSeven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 8:
                    System.out.print ( "eighth day of Christmas, \nmy true love" +
                            " gave to me\nEight maids a-milking,\n" +
                            "Seven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 9:
                    System.out.print ( "ninth day of Christmas, \nmy true love" +
                            " gave to me\nNine ladies dancing,\n" +
                            "Eight maids a-milking,\n" +
                            "Seven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 10:
                    System.out.print ( "tenth day of Christmas, \nmy true love" +
                            " gave to me\nTen lords a-leaping,\n" +
                            "Nine ladies dancing,\n" +
                            "Eight maids a-milking,\n" +
                            "Seven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 11:
                    System.out.print ( "eleventh day of Christmas, \nmy true love" +
                            " gave to me\nEleven pipers piping,\n" +
                            "Ten lords a-leaping,\n" +
                            "Nine ladies dancing,\n" +
                            "Eight maids a-milking,\n" +
                            "Seven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
                    break;

                case 12:
                    System.out.print ( "twelfth day of Christmas, \nmy true love" +
                            " gave to me\nTwelve drummers drumming,\n" +
                            "Eleven pipers piping,\n" +
                            "Ten lords a-leaping,\n" +
                            "Nine ladies dancing,\n" +
                            "Eight maids a-milking,\n" +
                            "Seven swans a-swimming,\n" +
                            "Six geese a-laying,\n" +
                            "Five golden rings\n" +
                            "Four calling birds,\n" +
                            "Three french hens,\n" +
                            "Two turtle doves \nand \nA " +
                            "partridge in a pear tree\n\n\n" );
                    day--;
            }//end switch


        }//end while

    }//end method main

}//end ChristmasSongpreciated.  Here is the code.....

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Because you are looping while day is greater than zero. If you only want the single result, remove the loop.

Another thing to consider though: you are retyping all of the gifts from other days on each case, which is unnecessary because the lines are additive. Each day adds a new line, but the previous lines are the same. Your code should reflect that.

Because you are looping while day is greater than zero. If you only want the single result, remove the loop.

Another thing to consider though: you are retyping all of the gifts from other days on each case, which is unnecessary because the lines are additive. Each day adds a new line, but the previous lines are the same. Your code should reflect that.

Thank you, been pulling my hair out. I thought I had to use a while loop to set up my switch and cases. I will now try to figure out what to do with the repetitive code. Thanks so much...

I'm not sure what your professor is expecting you to use here, but a fall-through switch works pretty well for the gifts due to the additive nature of each successive case:

System.out.print("On the ");
        
        String dayString = "";
        switch (day) {
            case 1:
                dayString="first";
                break;
            case 2:
                dayString="second";
                break;
            case 3:
                dayString="third";
                break;
        }

        System.out.print(dayString + " day of Christmas, \nmy true love gave to me\n");

        switch (day) {
            case 3:
                System.out.println("Three french hens,");
                // fall through
            case 2:
                System.out.println("Two turtle doves");
                // fall through
            case 1:
                if (day > 1) {
                    System.out.print("and ");
                }
                System.out.println("A partridge in a pear tree.");
        }//end switch

Arrays to hold the day names and the gifts would also work well and, in that case, a while loop would be appropriate to count down through the gifts.

Thank you Ezzaral- Arrays are still 2 chapters out although I am somewhat familiar with them from our Visual Basic class. I will play around with the code seed you supplied and see what I can come up with. Thanks again for your help. Sometimes trying to write code can make one feel like a complete idiot---hours of headache all over a short line of 16 unneeded characters.

This question has already been answered. Start a new discussion instead.