Menu
Coddy logo textTech

料金を割り勘にする

CoddyのKotlinジャーニー「基礎」セクションの一部。レッスン 44/93。

challenge icon

チャレンジ

簡単

前のレッスンでは、チップの金額と請求額の合計を計算しました。ここでは、請求額の合計をすべての人で分割するための最後の計算を追加します。

すでに用意されている変数を使って、次を計算します。

  • amountPerPerson: 請求額の合計を人数で割った値

出力を更新して、各人が支払うべき金額だけを表示します。

Welcome to Bill Split Calculator!
Each person pays: [amountPerPerson]

たとえば、入力が 100.0204 の場合、出力は次のようになります。

Welcome to Bill Split Calculator!
Each person pays: 30.0

計算は次のように行われます。チップは20.0、請求額の合計は120.0で、それを4人で割ると1人あたり30.0になります。

入力制約: 請求額とチップの割合は0以上であり、人数は正の整数です。数値入力は有効かつ有限です。

自分で試してみよう

fun main() {
    // TODO: Write your code below to print the welcome message
    println("Welcome to Bill Split Calculator!")
    
    val billAmount = readLine()!!.toDouble()
    val tipPercentage = readLine()!!.toInt()
    val numberOfPeople = readLine()!!.toInt()
    
    val tipAmount = billAmount * tipPercentage / 100
    val totalBill = billAmount + tipAmount
    
    println("Tip amount: $tipAmount")
    println("Total bill: $totalBill")
}

基礎のすべてのレッスン

自分で練習してみよう: Kotlinプレイグラウンド