Menu
Coddy logo textTech

Die Rechnung aufteilen

Teil des Abschnitts Grundlagen der R-Journey von Coddy — Lektion 36 von 78.

challenge icon

Aufgabe

Einfach

In der vorherigen Lektion haben Sie den Trinkgeldbetrag und den Gesamtbetrag 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 zahlen sollte, indem Sie den total durch die Anzahl der people teilen.

Speichern Sie dies in einer Variable namens per_person.

Zeigen Sie dann das Endergebnis im folgenden Format an:

Each person pays: $[per_person]

Verwenden Sie cat() für alle Ausgaben und fügen Sie bei Bedarf Zeilenumbruchzeichen \n ein.

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 cat() to display the welcome message
# Remember to use \n for new lines
cat("Welcome to the Bill Split Calculator!\nLet's split your bill fairly.\n")

# Read user inputs
con <- file("stdin", "r")
bill <- as.numeric(suppressWarnings(readLines(con, n = 1)))
tip_percent <- as.numeric(suppressWarnings(readLines(con, n = 1)))
people <- as.numeric(suppressWarnings(readLines(con, n = 1)))
close(con)

# Display confirmation message
cat("Bill: $", bill, "\n", sep = "")
cat("Tip: ", tip_percent, "%\n", sep = "")
cat("People: ", people, "\n", sep = "")

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

# Display calculation results
cat("Tip amount: $", tip_amount, "\n", sep = "")
cat("Total with tip: $", total, "\n", sep = "")

Alle Lektionen in Grundlagen