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.).
Re: Implementing the switch statement
Well for starters, are you familiar with how switch statements work?
Re: Implementing the switch statement
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.
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.
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?
Re: Implementing the switch statement
Write a small program and try it to see what happens.
Re: Implementing the switch statement
Code Java:
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
Re: Implementing the switch statement
Quote:
expecting an int variable.
Make the switch variable an int. All the case statements are int values. Get rid of the x variable.
Re: Implementing the switch statement
so completely take out the string = x; and delete the switch (x) but replace it with just int?
Re: Implementing the switch statement
What happened when you used an int value there instead a String?