Menu
Coddy logo textTech

チップと合計金額の計算

CoddyのRubyジャーニー「基礎」セクションの一部 — レッスン 36/88。

challenge icon

チャレンジ

簡単

前回のレッスンでは、請求額、チップのパーセンテージ、人数を取得しました。今回は、チップの金額とチップを含む合計金額を計算するロジックを追加しましょう。

確認メッセージを表示した後、以下を計算してください:

  1. チップの金額:請求額に、チップのパーセンテージを100で割った値を掛けます
  2. 合計金額:元の請求額にチップの金額を加算します

これらを tip_amounttotal という名前の変数に格納してください。

次に、計算結果を以下の形式で表示します:

Tip amount: $[tip_amount]
Total with tip: $[total]

すべての出力に puts を使用し、不要な末尾のゼロを避けるために %g を使用して数値をフォーマットしてください。

例えば、入力が 100204 の場合、完全な出力は以下のようになります:

Welcome to the Bill Split Calculator!
Let's split your bill fairly.
Bill: $100
Tip: 20%
People: 4
Tip amount: $20
Total with tip: $120

自分で試してみよう

# TODO: Write your code below
# Use puts to display the welcome message
puts "Welcome to the Bill Split Calculator!\nLet's split your bill fairly."

# Read user inputs
bill = gets.chomp.to_f
tip_percent = gets.chomp.to_f
people = gets.chomp.to_f

# Display confirmation message
puts "Bill: $%g" % bill
puts "Tip: %g%%" % tip_percent
puts "People: %g" % people

基礎のすべてのレッスン