我被分配編寫一個程序,用於在不使用循環的情況下將歌詞打印到「聖誕節十二天」(遞歸是好的),並且我認爲我擁有它但我不斷收到這個ArrayIndexOutOfBoundsException爲字符串數組賦值「聖誕節十二天」
java.lang.ArrayIndexOutOfBoundsException: -1
我修修補補和我的if語句和編號,但一對夫婦的朋友誰問我,我似乎無法查明問題。
public class TwelveDays{
public static void main(String[] args){
Twelve_Days(0);
}
public static void Twelve_Days (int day){
String[] days = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};
System.out.println("On the " + days[day] + " day of Christmas my true love game to me ");
Twelve_Gifts(day);
day++;
//if(day <=12);
if(day < 12){
Twelve_Days(day);
}
}
public static void Twelve_Gifts(int n){
String[] gifts = {"A partridge in a pear tree", "Two turtle doves", "Three French hens",
"Four Calling birds", "Five golven 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"};
System.out.println(gifts[n]);
if(n < 12){
Twelve_Gifts(n-1);
}
}
}
當然,任何幫助表示讚賞,謝謝一堆!