Menu
CoddyTech

連絡先を表示

CoddyのGDScriptジャーニー「ロジックとフロー」セクションの一部。レッスン 26/78。

challenge icon

チャレンジ

簡単

連絡先を1件表示する選択肢 2 に対応します。前の手順で書いた内容はすべて残してください。

view_contact(contact_book: Dictionary) を書きます。この関数は名前を読み込みます。名前が contact_book にあれば、次の形式でその連絡先の4つの詳細を表示します。

Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main St

それ以外の場合は、次のように表示します。

Contact not found!

ループ内で、"6" の分岐より前に、"2" に対する elif 分岐を追加し、view_contact(contact_book) を呼び出します。この関数はほかには何も表示しません。プロンプト文も表示しません。

自分で試してみよう

extends Node

func _ready():
	var contact_book := {}

	while true:
		display_menu()
		var choice := OS.read_string_from_stdin()
		if choice == "1":
			add_contact(contact_book)
		elif choice == "6":
			break

func display_menu() -> void:
	print("Contact Book Menu:")
	print("1. Add Contact")
	print("2. View Contact")
	print("3. Edit Contact")
	print("4. Delete Contact")
	print("5. List All Contacts")
	print("6. Exit")

func add_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		print("Contact already exists!")
		return
	var phone := OS.read_string_from_stdin()
	var email := OS.read_string_from_stdin()
	var address := OS.read_string_from_stdin()
	contact_book[contact_name] = {"phone": phone, "email": email, "address": address}
	print("Contact added successfully!")

ロジックとフローのすべてのレッスン

自分で練習してみよう: GDScriptオンラインコンパイラ