Print the song The Twelve Days of Christmas:
The Days class
Here is a program that solves this practice problem
for the final exam. (Solution in boldface.)
public class Days {
public static String[] days = { "",
"a partridge in a pear tree.",
"two turtle doves,",
"three french hens,",
"four calling birds,",
"five gold rings,",
"six geese a-laying,",
"seven swans a-swimming,",
"eight maids a-milking,",
"nine ladies dancing,",
"ten lords a-leaping,",
"eleven pipers piping,",
"twelve drummers drumming,"};
public static String[] numbers = {"",
"first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};
public static void main(String[] args) {
for (int i = 1; i <= 12; i++) {
System.out.println("On the " + numbers[i] +
" day of Christmas my true love gave to me");
for (int j = i; j >= 1; j--) {
if (i != 1 && j == 1) System.out.print("and ");
System.out.println(days[j]);
}
System.out.println();
}
}
}
Output:
On the first day of Christmas my true love gave to me
a partridge in a pear tree.
On the second day of Christmas my true love gave to me
two turtle doves,
and a partridge in a pear tree.
On the third day of Christmas my true love gave to me
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the fourth day of Christmas my true love gave to me
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the fifth day of Christmas my true love gave to me
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the sixth day of Christmas my true love gave to me
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the seventh day of Christmas my true love gave to me
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the eighth day of Christmas my true love gave to me
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the ninth day of Christmas my true love gave to me
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the eleventh day of Christmas my true love gave to me
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.
On the twelfth 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 gold rings,
four calling birds,
three french hens,
two turtle doves,
and a partridge in a pear tree.