Menu
Coddy logo textTech

비용 분할하기

Coddy Swift 여정의 기초 섹션에 포함된 레슨 — 86개 중 40번째.

challenge icon

챌린지

쉬움

이전 레슨에서 팁 금액과 팁을 포함한 총액을 계산했습니다. 이제 청구서를 나눌 때 각 사람이 지불해야 할 금액을 결정하는 최종 계산을 추가하세요.

이미 계산한 totalWithTipnumberOfPeople을 사용하여, 총액을 인원수로 나누어 각 사람이 지불해야 할 금액을 계산하세요.

나눗셈을 수행하기 전에 numberOfPeopleInt에서 Double로 변환하는 것을 잊지 마세요.

출력이 정확히 이 형식으로 출력되도록 업데이트하세요:

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

예를 들어, 입력값이 100.0, 20, 4인 경우 출력은 다음과 같아야 합니다:

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

직접 해보기

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)")
        }
    }
}

기초의 모든 레슨