エラー処理
CoddyのGDScriptジャーニー「基礎」セクションの一部。レッスン 89/92。
チャレンジ
簡単最後に、1から5以外のすべての選択肢を処理します。前の手順で書いた内容はすべて残し、次を出力します。
Invalid choice. Please try again.メッセージの後、ループはこれまでと同様に次の選択肢を読み取ります。
自分で試してみよう
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 == "4":
# すべての支出を消去する
expenses.clear()
print("All expenses cleared.")
elif choice == "5":
# プログラムを終了する
print("Exiting the Daily Expense Tracker. Goodbye!")
break
基礎のすべてのレッスン
自分で練習してみよう: GDScriptオンラインコンパイラ