Menu
CoddyTech

查看所有开支

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

challenge icon

挑战

简单

处理选项 2,查看所有支出。保留你在前面步骤中编写的所有内容。

如果支出数组为空,则打印:

No expenses recorded yet.

否则,打印 Your expenses:,然后按顺序打印每笔支出及其编号,编号从 1 开始。按顺序添加支出 23.1、35.5、99.99 和 15.2 时,输出为:

Your expenses:
1. 23.1
2. 35.5
3. 99.99
4. 15.2

以整数形式输入的支出会存储为 float,因此 12 会显示为 12.0。

亲手试试

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 == "5":
			# 退出程序
			print("Exiting the Daily Expense Tracker. Goodbye!")
			break

基础知识中的所有课

自己练习: GDScript 在线编译器