Menu
CoddyTech

清除所有内容

本课属于 Coddy GDScript 学习旅程的“基础知识”单元。第 88 课,共 92 课。

challenge icon

挑战

简单

处理选项 4,清除所有费用。保留你在前面步骤中写的所有内容。

从数组中删除所有费用,然后输出:

All expenses cleared.

之后,在添加新费用之前,选项 2 和 3 会输出 No expenses recorded yet.。

亲手试试

extends Node

func _ready():
	print("Welcome to the Daily Expense Tracker!")

	# 显示一次菜单
	print("\nMenu:")
	print("1. Add a new expense")
	print("2. View all expenses")
	print("3. Calculate total and average expense")
	print("4. Clear all expenses")
	print("5. Exit")

	# 用于存储支出的空数组
	var expenses: Array[float] = []

	while true:
		# 获取用户的选择
		var choice := OS.read_string_from_stdin()

		if choice == "1":
			# 添加一笔新支出
			var amount := float(OS.read_string_from_stdin())
			expenses.append(amount)
			print("Expense added successfully!")
		elif choice == "2":
			# 查看所有支出
			if expenses.is_empty():
				print("No expenses recorded yet.")
			else:
				print("Your expenses:")
				for i in range(expenses.size()):
					print(i + 1, ". ", expenses[i])
		elif choice == "3":
			# 计算总支出和平均支出
			if expenses.is_empty():
				print("No expenses recorded yet.")
			else:
				var total := 0.0
				for expense in expenses:
					total += expense
				var average := total / expenses.size()
				print("Total expense: ", total)
				print("Average expense: ", average)
		elif choice == "5":
			# 退出程序
			print("Exiting the Daily Expense Tracker. Goodbye!")
			break

基础知识中的所有课

自己练习: GDScript 在线编译器