Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Implementing the switch statement

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Implementing the switch statement

    This is the problem I have to do for a lab. I am not asking anyone to do the work for me but if they could just start it because I have no idea and my textbook doesn't help me very much with this one. I have been working on it for a week and can't even get started. I just need an example of how it would look or just how to switch the suffixes and have everything print. I have no idea how to start the switch statement. Thanks for any help given!

    2. Design and implement an application that prints the verses of the song “The Twelve Days of Christmas”, in which each verse adds one line. The two verses of the song are:
    On the 1st day of Christmas my true love gave to me
    A partridge in a pear tree
    On the 2nd day of Christmas my true love gave to me
    Two turtles doves and,
    A partridge in a pear tree
    Use a switch statement in a loop to control which lines get printed. Hint: Order the cases carefully and avoid the break statement. Use a separate switch statement to put the appropriate suffix on the day number (1st, 2nd, 3rd, etc.).


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Implementing the switch statement

    Well for starters, are you familiar with how switch statements work?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing the switch statement

    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Implementing the switch statement

    i did look at that but I don't understand how I can print the first line, then the second line but changing it to "2nd day" and reprinting the first line.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing the switch statement

    Look at what order the song lines are in as new lines are added:
    1
    then
    2
    1
    then
    3
    2
    1

    Then think what happens in a switch statement when a case statement does not have a break:
    Hint: it falls through and does the next case that is below it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Implementing the switch statement

    So the switch statement without a break will print 3 when i call it and then it will also print 2 and 1 after it or do I need to print them backwards so 12 is the first case and 1 is the last case and call the last case first so it will then print the last one, then the second to last and the last, and so on?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing the switch statement

    Write a small program and try it to see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Implementing the switch statement

    public class StudentInformation
    {
        public static void main (String [] args)
        {
            String x;
            for (int n = 12; n >= 1; n--)
            {
                System.out.println ("On the " + n + " day of Christmas my true love gave to me");
                System.out.println (x);
     
                switch (x)
                {
                case 1 : System.out.println ("Twelve drummers drumming,");
                case 2 : System.out.println ("Eleven pipers piping,");
                case 3 : System.out.println ("Ten lords a leaping,");
                case 4 : System.out.println ("Nine ladies dancing,");
                case 5 : System.out.println ("Eight maids a milking,");
                case 6 : System.out.println ("Seven swans a swimming,");
                case 7 : System.out.println ("Six gesse a laying,");
                case 8 : System.out.println ("Five golden rings,");
                case 9 : System.out.println ("Four calling birds,");
                case 10 : System.out.println ("Three french hens,");
                case 11 : System.out.println ("Two turtle doves and,");
                case 12 : System.out.println ("A partridge in a pear tree");
                }
            }
        }
    }

    This is my code and I get an error message saying that the "switch (x)" is not a compatible type and it is expecting an int variable.
    Not sure where to go from here

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing the switch statement

    expecting an int variable.
    Make the switch variable an int. All the case statements are int values. Get rid of the x variable.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Implementing the switch statement

    so completely take out the string = x; and delete the switch (x) but replace it with just int?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing the switch statement

    What happened when you used an int value there instead a String?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. [SOLVED] (beginner) Switch Statement
    By georgewbw in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 11:18 PM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM