您的位置:首页 > 其它

歌曲《圣诞节十二天》歌词打印("The Twelve Days of Christmas" Song)

2016-06-24 12:37 483 查看
对于有一定规律的文本,如《圣诞节十二天》这首歌,可以用程序完美输出。

不用打字、不用复制粘贴?对!只需执行代码,电脑将为你自动生成八百多字的歌词,一字不差


注意:

1.字符串数组中的元素要初始化,否则,打印时会显示“null”

2.对于个别词组,可能在不同段落(如,第一段和最后一段)的显示有细微差别,可通过增加判断条件,单独定义。(本例中在case中增加if, else if语句实现)

代码如下:

//JHTP Exercise 5.29: "The Twelve Days of Christmas" Song
//by pandenghuang@163.com
/**(“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!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: