전체 지출 내역 보기
Coddy Swift 여정의 기초 섹션에 포함된 레슨 — 86개 중 80번째.
챌린지
쉬움이전 레슨에서 "Add expense" 기능을 구현했습니다. 이제 옵션 "2"에 대한 "View all expenses" 기능을 구현해 보세요.
옵션 "2"를 처리하도록 코드를 수정하세요:
- 사용자가
"2"를 입력하면,expenses배열이 비어 있는지 확인합니다. - 배열이 비어 있으면,
No expenses recorded.를 출력합니다. - 배열에 지출 내역이 있으면,
Your expenses:를 출력한 후 각 지출 내역을- $X.XX형식(여기서 X.XX는 지출 금액)으로 각 줄에 하나씩 출력합니다. - 옵션
"3"과"4"는 여전히Coming soon...을 출력해야 합니다. - 옵션
"1"과"5"는 이전과 동일하게 작동해야 합니다.
예상 동작:
- 지출 내역이 없을 때
"2"를 입력하면,No expenses recorded.를 출력합니다. - 지출 내역이 있을 때
"2"를 입력하면, 헤더를 출력하고 각 지출 내역을 대시와 달러 기호와 함께 나열합니다.
상호작용 예시:
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
2
No expenses recorded.
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Enter expense amount:
25.50
Expense added!
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Enter expense amount:
10.00
Expense added!
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
2
Your expenses:
- $25.5
- $10.0
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
var expenses: [Double] = []
while true {
// 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")
let choice = readLine()
if choice == "1" {
print("Enter expense amount:")
if let input = readLine(), let amount = Double(input) {
expenses.append(amount)
print("Expense added!")
}
} else if choice == "2" {
print("Coming soon...")
} else if choice == "3" {
print("Coming soon...")
} else if choice == "4" {
print("Coming soon...")
} else if choice == "5" {
print("Goodbye!")
break
} else {
print("Coming soon...")
}
}