Xóa tất cả
Thuộc phần Kiến thức nền tảng trong hành trình Swift của Coddy. Bài học 82 trên 86.
Thử thách
DễTrong bài học trước, bạn đã triển khai tính năng "Show total and average". Bây giờ, hãy triển khai tính năng "Clear all expenses" cho tùy chọn "4".
Sửa code để xử lý tùy chọn "4":
- Khi người dùng nhập
"4", xóa tất cả phần tử khỏi mảngexpenses - In ra
All expenses cleared!sau khi xóa mảng - Tất cả tùy chọn khác (
"1","2","3"và"5") vẫn tiếp tục hoạt động như trước
Gợi ý: Dùng phương thức removeAll() để xóa tất cả phần tử khỏi mảng.
Hành vi mong đợi:
- Khi nhập
"4", xóa mảng expenses và xác nhận bằng một thông báo - Sau khi xóa, xem chi phí sẽ hiển thị
No expenses recorded. - Sau khi xóa, xem tổng và trung bình sẽ hiển thị
No expenses to calculate.
Tương tác mẫu:
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Enter expense amount:
15.0
Expense added!
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
1
Enter expense amount:
25.0
Expense added!
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
2
Your expenses:
- $15.0
- $25.0
1. Add expense
2. View all expenses
3. Show total and average
4. Clear all expenses
5. Exit
4
All expenses cleared!
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
5
Goodbye!Tự làm thử
// 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" {
if expenses.isEmpty {
print("No expenses recorded.")
} else {
print("Your expenses:")
for expense in expenses {
print("- $\(expense)")
}
}
} else if choice == "3" {
if expenses.isEmpty {
print("No expenses to calculate.")
} else {
let total = expenses.reduce(0, +)
let average = total / Double(expenses.count)
print("Total: $\(total)")
print("Average: $\(average)")
}
} else if choice == "4" {
print("Coming soon...")
} else if choice == "5" {
print("Goodbye!")
break
} else {
print("Coming soon...")
}
}Tất cả bài học trong Kiến thức nền tảng
4Các toán tử, phần 1
Toán tử số họcToán tử moduloPhép gán kết hợpÔn tập: phép tính đơn giảnToán tử so sánh7Nhập xuất cơ bản
Hàm in raNội suy chuỗiĐầu vào từ dòngChuyển đổi kiểu dữ liệuÔn tập đến 120Ôn tập đúng hay sai3Giá trị tùy chọn
Giá trị tùy chọn là gìMở gói bằng if letGuard letToán tử gộp nilÔn tập: mở gói an toàn6Ra quyết định
Câu lệnh ifIf và elseÔn tập: máy tính đơn giảnCâu lệnh switchSwitch với khoảng giá trị9Vòng lặp
Vòng lặp for-inVòng lặp whileVòng lặp repeat-whileBreakContinueÔn tập: giai thừaDải giá trị trong vòng lặpVòng lặp lồng nhauÔn tập: đầu vào động12Kiến thức cơ bản về mảng
Khai báo mảngTruy cập phần tửChỉnh sửa mảngPhương thức của mảngÔn tập: Danh sách sản phẩmÔn tập: Mảng đảo ngượcTuple15Trình theo dõi chi tiêu hằng ngày
Tổng quan về dự ánThoát chương trìnhTự luyện tập: Playground Swift