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