合計と平均
CoddyのGDScriptジャーニー「基礎」セクションの一部。レッスン 87/92。
チャレンジ
簡単選択肢 3 を処理し、支出の合計と平均を計算します。前の手順で書いた内容はすべて残してください。
支出の配列が空の場合は、次のように出力します。
No expenses recorded yet.それ以外の場合は、合計と平均を2行で出力します。支出が 100、200、300 の場合、出力は次のようになります。
Total expense: 600.0
Average expense: 200.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 == "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 == "5":
# プログラムを終了する
print("Exiting the Daily Expense Tracker. Goodbye!")
break
基礎のすべてのレッスン
自分で練習してみよう: GDScriptオンラインコンパイラ