This is in java. Write a program that prints the verses of the song “The Twelve
ID: 3939735 • Letter: T
Question
This is in java.
Write a program that prints the verses of the song “The Twelve Days of Christmas,” in which each verse adds one line. The first 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 turtle 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, 4th, …). The final verse of the song involves all 12 days, as follows: On the 12th day of Christmas my true love gave to me Twelve drummers drumming Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, and A partridge in a pear tree.
Explanation / Answer
Here is the code what you have asked for and please do let me know if any errors occurs.
public class Main
{
public static void main(String[] args)
{
String[] arrLyrics;
arrLyrics = new String[13];
arrLyrics[0] = "On the % day of Christmas, my true love gave to me";
arrLyrics[1] = "A partridge in a pear tree.";
arrLyrics[2] = "Two turtle doves, and";
arrLyrics[3] = "Three French hens,";
arrLyrics[4] = "Four calling birds,";
arrLyrics[5] = "Five golden rings,";
arrLyrics[6] = "Six geese a laying,";
arrLyrics[7] = "Seven swans a swimming,";
arrLyrics[8] = "Eight maids a milking,";
arrLyrics[9] = "Nine ladies dancing,";
arrLyrics[10] = "Ten lords a leaping,";
arrLyrics[11] = "Eleven pipers piping,";
arrLyrics[12] = "Twelve drummers drumming,";
int loopAmount = 13;
for (int i=1; i<loopAmount; i++)
{
String day;
String dailyLyric;
if(i==1)
{
day = "st";
} else if(i==2)
{
day = "nd";
} else if(i==3)
{
day = "ed";
}
else
{
day = "th";
}
dailyLyric = arrLyrics[0].replace("%", i + day);
System.out.println(dailyLyric);
switch(i)
{
case 12:
System.out.println(arrLyrics[12]);
case 11:
System.out.println(arrLyrics[11]);
case 10:
System.out.println(arrLyrics[10]);
case 9:
System.out.println(arrLyrics[9]);
case 8:
System.out.println(arrLyrics[8]);
case 7:
System.out.println(arrLyrics[7]);
case 6:
System.out.println(arrLyrics[6]);
case 5:
System.out.println(arrLyrics[5]);
case 4:
System.out.println(arrLyrics[4]);
case 3:
System.out.println(arrLyrics[3]);
case 2:
System.out.println(arrLyrics[2]);
case 1:
System.out.println(arrLyrics[1]);
System.out.println();
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.