题目:
解法1:
package exercism;
public class TwelveDays {
void songLyrics() {
String[] s1 = {"first","second","third","forth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"};
String[] s2 = {"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, "};
for (int i = 1; i <= s1.length; i++) {
System.out.print("On the " + s1[i-1] + " day of Christmas my true love gave to me: ");
for (int j = i; j >=1; j--) {
System.out.print(j == 1 && i != 1? "and " + s2[j - 1] : s2[j - 1]);
}
System.out.println();
}
}
}
解法二
import java.util.Arrays;
class TwelveDays {
public static final String[] verses = {
"On the first day of Christmas my true love gave to me, a Partridge in a Pear Tree.\n",
"On the second day of Christmas my true love gave to me, two Turtle Doves, and a Partridge in a Pear Tree.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n",
"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.\n"
};
String verse(int verseNumber) {
return verses[verseNumber - 1];
}
String verses(int startVerse, int endVerse) {
return String.join("\n", Arrays.copyOfRange(verses, startVerse - 1, endVerse));
}
String sing() {
return String.join("\n", verses);
}
}
总结
解法一:
寻找一般规律,将规律表述出来显得更加简练,但可读性稍逊解法二,但如果是面对大宗规模,解法二可能就显得比较繁赘
解法二:
使用到了String类的join方法和Arrays类的copyofRange方法,显得更加简练