Flutter作業#03 — 定義 function,印出歌詞

作業:

#64 定義 function,印出讓自己一秒落淚的情歌歌詞

這次來用 Lindsey Sterling 的 <Something Wild> 歌詞來練習。首先拆分歌詞各段落:

You had your maps drawn
You had other plans
To hang your hopes on
Every road they led you down felt so wrong
So you found another way

You’ve got a big heart
The way you see the world
It got you this far
You might have some bruises
And a few of scars
But you know you’re gonna be okay

這一段 verse 沒有重複段落,可直接印出:

void verse1() { //因為只需要 print 出文字不需要 return,故使用 void 開頭
print(
"""You had your maps drawn
You had other plans
To hang your hopes on
Every road they led you down felt so wrong
So you found another way
You've got a big heart
The way you see the world
It got you this far
You might have some bruises
And a few of scars
But you know you're gonna be okay""",
);
}

Even though you’re scared
You’re stronger than you know

If you’re lost out where the lights are blinding
Caught in all, the stars are hiding
That’s when something wild calls you home, home

If you face the fear that keeps you frozen
Chase the sky into the ocean
That’s when something wild calls you home, home

這一段副歌比較複雜一點,每一小段會各自單獨重複、有時候還會有點不同的改動:

void stronger(bool evenThough) { //用 bool 來決定改動的歌詞部分
String chore1 = "Even though";
String chore2 = "Even if";
String trailing = """you're scared
You're stronger than you know""";

if (evenThough == true) { //記得要 "=="。這裡少輸入一個 "=" 不會自動提醒!!然後就只會一直觸發 evenThough == true 的情況😂
print("$chore1 $trailing"); //這裡試著用 $ 呼叫
} else {
print(chore2 + " " + trailing); //這邊則嘗試用 + 連接
}
}


void callsYouHome() { //這一段會在下面兩個 void 中重複,故特別 extract 出來。
print(
"That's when something wild calls you home, home",
);
}


void lost() {
print(
"If you're lost out where the lights are blinding\nCaught in all, the stars are hiding");
callsYouHome(); //呼叫剛剛 extract 的 func
}


void fear() {
print(
"If you face the fear that keeps you frozen\nChase the sky into the ocean");
callsYouHome(); //呼叫剛剛 extract 的 func
}

Sometimes the past can
Make the ground beneath you feel like quicksand
You don’t have to worry
Reach for my hand
And I know you’re gonna be okay
You’re gonna be okay

這一段是單純的 verse 直接印出:

void verse2() {
print(
"""Sometimes the past can
Make the ground beneath you feel like quicksand
You don't have to worry
Reach for my hand
And I know you're gonna be okay
You're gonna be okay""",
);
}

Calls you home
Calls you home
Calls you home
Calls you home

這一段落剛好重複了四句一樣的歌詞,故練習使用 for loop:

void hoemLoop() {
String line = "Calls you home";

for (var i = 0; i < 4; i++) { //輸入 for 就有自動完成的選項出現,選擇後就自動出現這列程式碼啦!只要改動要重複的次數即可。
print(line);
}
}

最後在 main() 裡呼叫所有的 func 就完成啦!

void main(List<String> arguments) {
verse1();
stronger(true);
lost();
fear();
verse2();
stronger(false);
lost();
fear();
hoemLoop();
lost();
lost();
fear();
}

成果:

歡迎搭配歌曲服用~

--

--