チップと合計の計算
CoddyのRジャーニー「基礎」セクションの一部 — レッスン 35/78。
チャレンジ
簡単前のレッスンでは、請求額、チップの割合、および人数を収集しました。次に、チップの金額とチップを含む合計請求額を計算するためのロジックを追加します。
確認メッセージを表示した後、以下を計算してください:
- チップの金額:請求額に、チップの割合を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 = "")