팁과 총액 계산하기
Coddy Swift 여정의 기초 섹션에 포함된 레슨. 86개 중 39번째.
챌린지
쉬움이전 레슨에서는 사용자에게서 총 청구 금액, 팁 비율, 인원수를 입력받았습니다. 이제 팁 금액과 팁을 포함한 총 청구 금액을 계산하는 로직을 추가하세요.
이미 가지고 있는 값을 사용해 다음을 계산하세요.
- 팁 금액: 총 청구 금액에 팁 비율을 곱한 뒤 100으로 나눈 값
- 팁을 포함한 총액: 총 청구 금액에 팁 금액을 더한 값
계산하기 전에 팁 비율을 Int에서 Double로 변환해야 합니다.
출력이 다음 형식과 정확히 일치하도록 업데이트하세요.
Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]
Tip amount: [tipAmount]
Total with tip: [totalWithTip]예를 들어 입력이 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직접 해보기
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) {
print("Bill: \(billTotal)")
print("Tip: \(tipPercentage)%")
print("People: \(numberOfPeople)")
}
}
}기초의 모든 레슨
직접 연습해 보세요: Swift 플레이그라운드