プログラムの終了
CoddyのSwiftジャーニー「基礎」セクションの一部 — レッスン 78/86。
チャレンジ
簡単前回のレッスンでは、支出トラッカーのメニューを表示しました。今回は、ユーザーが終了を選択するまでメニューを繰り返し表示し、ユーザーの入力を読み取るループを追加しましょう。
コードを以下のように修正してください:
- 支出を保存するために、
[Double]型の空の配列expensesを作成します - プログラムを実行し続けるために
while trueループを使用します - ループの中で、メニューを表示します(前回と同様)
readLine()を使用してユーザーの選択を読み取ります- ユーザーが
"5"を入力した場合は、Goodbye!と表示し、breakを使用してループを抜けます - それ以外の入力に対しては、
Coming soon...と表示します(他のオプションは後のレッスンで実装します)
期待される動作:
- ユーザーが
"5"を入力するまで、メニューが繰り返し表示されます "5"が入力されると、Goodbye!と表示してループを終了します- 選択肢
"1"、"2"、"3"、"4"(またはその他の入力)の場合は、Coming soon...と表示して再びメニューを表示します
インタラクションの例:
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Coming soon...
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
5
Goodbye!自分で試してみよう
// Daily Expense Tracker - Menu Display
// TODO: Write your code below to print the menu options
// Each option should be on its own line with the format: "number. option text"
print("1. Add expense")
print("2. View all expenses")
print("3. Show total and average")
print("4. Clear all expenses")
print("5. Exit")