팁과 총액 계산하기
Coddy R 여정의 기초 섹션에 포함된 레슨 — 78개 중 35번째.
챌린지
쉬움이전 레슨에서는 청구 금액, 팁 백분율, 인원수를 수집했습니다. 이제 팁 금액과 팁을 포함한 총 청구 금액을 계산하는 로직을 추가하세요.
확인 메시지를 표시한 후, 다음을 계산하세요:
- 팁 금액: 청구 금액에 팁 백분율을 100으로 나눈 값을 곱합니다.
- 총 청구 금액: 원래 청구 금액과 팁 금액을 더합니다.
이 값들을 tip_amount와 total이라는 이름의 변수에 저장하세요.
그런 다음 계산 결과를 다음 형식으로 표시하세요:
Tip amount: $[tip_amount]
Total with tip: $[total]모든 출력에는 cat()을 사용하고 필요한 곳에 줄바꿈 문자 \n을 포함하세요.
예를 들어, 입력값이 100, 20, 4인 경우 전체 출력은 다음과 같아야 합니다:
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직접 해보기
# 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 = "")