すべてクリアする
CoddyのGDScriptジャーニー「基礎」セクションの一部。レッスン 88/92。
チャレンジ
簡単選択肢 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オンラインコンパイラ