Menu
CoddyTech

计算小费和总额

本课属于 Coddy Kotlin 学习旅程的“基础知识”单元。第 43 课,共 93 课。

challenge icon

挑战

简单

在上一课中,你收集了账单金额、小费百分比和人数。现在,添加小费计算逻辑,计算小费金额和账单总额。

使用已有的变量,计算:

  • tipAmount:根据账单金额和小费百分比计算的小费
  • totalBill:原始账单金额与小费金额的总和

要计算小费金额,请将账单金额乘以小费百分比,再除以 100。

更新输出,显示小费金额和账单总额,而不是原始输入:

Welcome to Bill Split Calculator!
Tip amount: [tipAmount]
Total bill: [totalBill]

例如,如果输入是 100.0、20 和 4,输出应为:

Welcome to Bill Split Calculator!
Tip amount: 20.0
Total bill: 120.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()
    
    println("Bill amount: $billAmount")
    println("Tip percentage: $tipPercentage")
    println("Number of people: $numberOfPeople")
}

基础知识中的所有课

自己练习: Kotlin 在线编辑器