금액 나누기
Coddy R 여정의 기초 섹션에 포함된 레슨 — 78개 중 36번째.
챌린지
쉬움이전 레슨에서는 팁 금액과 총 청구 금액을 계산했습니다. 이제 마지막 단계인 전체 금액을 모든 사람에게 나누는 기능을 추가해 보세요.
팁 금액과 팁이 포함된 총액을 표시한 후, total을 people 수로 나누어 각 사람이 지불해야 할 금액을 계산합니다.
이 값을 per_person이라는 변수에 저장하세요.
그런 다음 최종 결과를 다음 형식으로 표시합니다:
Each person pays: $[per_person]모든 출력에는 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
Each person pays: $30직접 해보기
# 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 = "")