Menu
Coddy logo textTech

Die Rechnung aufteilen

Teil des Abschnitts Grundlagen der Ruby-Journey von Coddy — Lektion 37 von 88.

challenge icon

Aufgabe

Einfach

In der vorherigen Lektion haben Sie den Trinkgeldbetrag und die Gesamtrechnung berechnet. Fügen Sie nun den letzten Schritt hinzu: die Aufteilung des Gesamtbetrags auf alle Personen.

Nachdem Sie den Trinkgeldbetrag und den Gesamtbetrag inklusive Trinkgeld angezeigt haben, berechnen Sie, wie viel jede Person bezahlen sollte, indem Sie total durch die Anzahl der people teilen.

Speichern Sie dies in einer Variablen namens per_person.

Zeigen Sie dann das Endergebnis im folgenden Format an:

Each person pays: $[per_person]

Verwenden Sie puts für alle Ausgaben und formatieren Sie Zahlen mit %g, um unnötige nachfolgende Nullen zu vermeiden.

Wenn die Eingaben beispielsweise 100, 20 und 4 sind, sollte die vollständige Ausgabe wie folgt aussehen:

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
Each person pays: $30

Probier es selbst

# 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

# Calculate tip amount and total
tip_amount = bill * tip_percent / 100
total = bill + tip_amount

# Display calculation results
puts "Tip amount: $%g" % tip_amount
puts "Total with tip: $%g" % total

Alle Lektionen in Grundlagen