Menu
CoddyTech

Tìm kiếm liên hệ

Thuộc phần Logic và luồng chương trình trong hành trình Lua của Coddy. Bài học 45 trên 54.

challenge icon

Thử thách

Dễ

Triển khai chức năng tìm kiếm cho ứng dụng danh sách liên hệ của bạn. Khi người dùng nhập search, chương trình sẽ yêu cầu nhập tên và hiển thị tất cả liên hệ khớp với tên đó.

Tiếp nối thử thách trước, hãy bổ sung chức năng xử lý lệnh search. Duyệt qua bảng contacts để tìm những liên hệ có tên khớp với từ khóa tìm kiếm. Hiển thị tất cả kết quả khớp hoặc thông báo cho người dùng nếu không tìm thấy liên hệ nào.

Yêu cầu:

  • Giữ lại toàn bộ code từ thử thách trước (khởi tạo, hiển thị menu, vòng lặp chính, chức năng thêm và chức năng liệt kê)
  • Khi người dùng nhập search, hãy in ra: Enter name to search:
  • Đọc từ khóa tìm kiếm bằng io.read()
  • Dùng ipairs() để duyệt qua bảng contacts
  • Với mỗi liên hệ, kiểm tra xem trường name có khớp chính xác với từ khóa tìm kiếm hay không
  • Nếu tìm thấy kết quả khớp, hãy in theo định dạng: Found: [name] - [phone]
  • Nếu không tìm thấy kết quả nào sau khi kiểm tra tất cả liên hệ, hãy in: Contact not found.
  • Các lệnh add, list và quit vẫn phải hoạt động như trước
  • Các lệnh khác vẫn phải in ra Command not yet implemented

Ví dụ tương tác:

Welcome to Contact List Manager!

Available Commands:
add - Add a new contact
list - Display all contacts
search - Search for a contact
delete - Delete a contact
quit - Exit the program

Enter command:
add
Enter name:
Alice
Enter phone number:
555-1234
Contact added successfully!

Enter command:
add
Enter name:
Bob
Enter phone number:
555-5678
Contact added successfully!

Enter command:
search
Enter name to search:
Alice
Found: Alice - 555-1234

Enter command:
search
Enter name to search:
Charlie
Contact not found.

Enter command:
quit
Goodbye!

Tự làm thử

-- Contact List Manager
local contacts = {}

-- Display welcome message
print("Welcome to Contact List Manager!")
print()

-- Display menu
print("Available Commands:")
print("add - Add a new contact")
print("list - Display all contacts")
print("search - Search for a contact")
print("delete - Delete a contact")
print("quit - Exit the program")
print()

-- Main loop
while true do
    print("Enter command:")
    local command = io.read()
    
    if command == "add" then
        print("Enter name:")
        local name = io.read()
        print("Enter phone number:")
        local phone = io.read()
        
        table.insert(contacts, {name = name, phone = phone})
        print("Contact added successfully!")
        print()
        
    elseif command == "list" then
        if #contacts == 0 then
            print("No contacts found.")
        else
            print("Contact List:")
            for i, contact in ipairs(contacts) do
                print(i .. ". " .. contact.name .. " - " .. contact.phone)
            end
        end
        print()
        
    elseif command == "quit" then
        print("Goodbye!")
        break
        
    else
        print("Command not yet implemented")
        print()
    end
end

Tất cả bài học trong Logic và luồng chương trình

Tự luyện tập: Trình biên dịch Lua online