1. 程式人生 > >歌曲《聖誕節十二天》歌詞列印("The Twelve Days of Christmas" Song)

歌曲《聖誕節十二天》歌詞列印("The Twelve Days of Christmas" Song)

對於有一定規律的文字,如《聖誕節十二天》這首歌,可以用程式完美輸出。

不用打字、不用複製貼上?對!只需執行程式碼,電腦將為你自動生成八百多字的歌詞,一字不差微笑

注意:

1.字串陣列中的元素要初始化,否則,列印時會顯示“null”

2.對於個別片語,可能在不同段落(如,第一段和最後一段)的顯示有細微差別,可通過增加判斷條件,單獨定義。(本例中在case中增加if, else if語句實現)

程式碼如下:

//JHTP Exercise 5.29: "The Twelve Days of Christmas" Song
//by [email protected]
/**(“The Twelve Days of Christmas” Song) Write an application that uses repetition and
 *  switch statements to print the song “The Twelve Days of  Christmas.” One switch 
 *  statement should be used to print the day (“first,” “second,” and so on). 
 *  A separate switch statement should be used to print the remainder of each verse. 
 *  Visit the website en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song) for the
 *   lyrics of the song.*/

public class SongTDC 
{
public static void main(String[] args)
{
	String[] day=new String[12];
	String[] gift=new String[12];
	for (int i=1;i<=12;i++)
		gift[i-1]="";
	
	for (int i=1;i<=12;i++){
		switch (i){
		case  1:
			day[i-1]="first";
			break;
		case  2:
			day[i-1]="second";
			break;
		case  3:
			day[i-1]="third";
			break;
		case  4:
			day[i-1]="fourth";
			break;
		case  5:
			day[i-1]="fifth";
			break;
		case  6:
			day[i-1]="sixth";
			break;
		case  7:
			day[i-1]="seventh";
			break;
		case  8:
			day[i-1]="eighth";
			break;
		case  9:
			day[i-1]="ninth";
			break;
		case  10:
			day[i-1]="tenth";
			break;
		case  11:
			day[i-1]="eleventh";
			break;
		case  12:
			day[i-1]="twelfth";
			break;
		}	
		
		switch (i){
		case  12:
			gift[i-1]="Twelve drummers drumming, ";
		case  11:
			gift[i-1]+="Eleven pipers piping, ";
		case  10:
			gift[i-1]+="Ten lords a-leaping, ";
		case  9:
			gift[i-1]+="Nine ladies dancing, ";
		case  8:
			gift[i-1]+="Eight maids a-milking, ";
		case  7:
			gift[i-1]+="Seven swans a-swimming, ";
		case  6:
			gift[i-1]+="Six geese a-laying, ";
		case  5:
			gift[i-1]+="Five golden rings, ";
		case  4:
			gift[i-1]+="Four calling birds, ";
		case  3:
			gift[i-1]+="Three French hens, ";
		case  2:
			gift[i-1]+="Two turtle doves, ";
		case  1:
			if(i!=1 &&i!=12)
			gift[i-1]+="And A partridge in a pear tree.";
			else if(i==1)
				gift[i-1]+="A partridge in a pear tree.";
			else
				gift[i-1]+="And A partridge in a pear tree!";
		}
	
			System.out.printf("On the %s day of Christmas, "
					+ "my true love sent to me: %s\n\n",day[i-1],gift[i-1]);
	} 
} 
}

執行結果:

On the first day of Christmas, my true love sent to me: A partridge in a pear tree.

On the second day of Christmas, my true love sent to me: Two turtle doves, And A partridge in a pear tree.

On the third day of Christmas, my true love sent to me: Three French hens, Two turtle doves, And A partridge in a pear tree.

On the fourth day of Christmas, my true love sent 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 sent to me: Five golden 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 sent to me: Six geese a-laying, Five golden 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 sent to me: 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.

On the eighth day of Christmas, my true love sent to me: 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.

On the ninth day of Christmas, my true love sent to me: 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.

On the tenth day of Christmas, my true love sent to me: 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.

On the eleventh day of Christmas, my true love sent 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 golden 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 sent 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!