Menu
CoddyTech

Rechnung aufteilen

Teil des Abschnitts Grundlagen der Swift-Journey von Coddy. Lektion 40 von 86.

challenge icon

Aufgabe

Einfach

In der vorherigen Lektion hast du den Trinkgeldbetrag und den Gesamtbetrag inklusive Trinkgeld berechnet. Füge nun die abschließende Berechnung hinzu, um zu bestimmen, wie viel jede Person beim Aufteilen der Rechnung bezahlt.

Berechne mithilfe von totalWithTip, das du bereits berechnet hast, und numberOfPeople den Betrag, den jede Person bezahlen sollte, indem du den Gesamtbetrag durch die Anzahl der Personen teilst.

Denke daran, numberOfPeople vor der Division von Int in Double umzuwandeln.

Aktualisiere deine Ausgabe so, dass sie genau in diesem Format ausgegeben wird:

Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]
Tip amount: [tipAmount]
Total with tip: [totalWithTip]
Each person pays: [amountPerPerson]

Wenn die Eingaben beispielsweise 100.0, 20 und 4 sind, sollte die Ausgabe wie folgt aussehen:

Welcome to the Bill Split Calculator!
Bill: 100.0
Tip: 20%
People: 4
Tip amount: 20.0
Total with tip: 120.0
Each person pays: 30.0

Probier es selbst

print("Welcome to the Bill Split Calculator!")

if let billInput = readLine(), let billTotal = Double(billInput) {
    if let tipInput = readLine(), let tipPercentage = Int(tipInput) {
        if let peopleInput = readLine(), let numberOfPeople = Int(peopleInput) {
            let tipAmount = billTotal * Double(tipPercentage) / 100
            let totalWithTip = billTotal + tipAmount
            
            print("Bill: \(billTotal)")
            print("Tip: \(tipPercentage)%")
            print("People: \(numberOfPeople)")
            print("Tip amount: \(tipAmount)")
            print("Total with tip: \(totalWithTip)")
        }
    }
}

Alle Lektionen in Grundlagen

Übe selbstständig: Swift-Playground