【13】 Kotlin 小案例,计算器

 java 代码如果要实现的话 代码量更多些。

下面直接看kotlin的。

package com.yzdzy.kotlin.calc

fun main(args: Array<String>) {
    println("请输入算是 列如 3 + 4")
    val input = readLine()
    if (input != null) {
        val splits = input.split(" ")
        val arg1 = splits[0].toDouble()
        val op = splits[1]
        val arg2 = splits[2].toDouble()
        print("$arg1 $op $arg2 = ${Operator(op).apply(arg1,arg2)}")
    }

}

class Operator(op: String) {
    val opFun: (left: Double, right: Double) -> Double

    init {
        opFun = when (op) {
            "+" -> { l, r -> l + r }
            "-" -> { l, r -> l - r }
            "*" -> { l, r -> l * r }
            "/" -> { l, r -> l / r }
            "%" -> { l, r -> l % r }
            else -> {
                throw UnsupportedOperationException(op)
            }
        }
    }

    fun apply(left: Double, right: Double): Double {
        return opFun(left,right)
    }
}

输出

请输入算是 列如 3 + 4
3 + 4
3.0 + 4.0 = 7.0

优化改进成一直运算。

package com.yzdzy.kotlin.calc

fun main(args: Array<String>) {
    while (true) {
        println("请输入算是 列如 3 + 4")
        val input = readLine() ?: break
        val splits = input.split(" ")
        val arg1 = splits[0].toDouble()
        val op = splits[1]
        val arg2 = splits[2].toDouble()
        println("$arg1 $op $arg2 = ${Operator(op).apply(arg1, arg2)}")
        println("再来一次?[Y]")
        val cmd = readLine()
        if (cmd == null || cmd.toLowerCase() != "y") {
            break
        }
    }

}

class Operator(op: String) {
    val opFun: (left: Double, right: Double) -> Double

    init {
        opFun = when (op) {
            "+" -> { l, r -> l + r }
            "-" -> { l, r -> l - r }
            "*" -> { l, r -> l * r }
            "/" -> { l, r -> l / r }
            "%" -> { l, r -> l % r }
            else -> {
                throw UnsupportedOperationException(op)
            }
        }
    }

    fun apply(left: Double, right: Double): Double {
        return opFun(left, right)
    }
}
package com.yzdzy.kotlin.calc

fun main(args: Array<String>) {
    while (true) {
        println("请输入算是 列如 3 + 4")

        try {
            val input = readLine() ?: break
            val splits = input.trim().split(" ")
            if (splits.size < 2) {
                throw IllegalArgumentException("参数个数不对")
            }
            val arg1 = splits[0].toDouble()
            val op = splits[1]
            val arg2 = splits[2].toDouble()
            println("$arg1 $op $arg2 = ${Operator(op).apply(arg1, arg2)}")
        } catch (e: NumberFormatException) {
            println("您确定输入的是数字吗")
        } catch (e: IllegalArgumentException) {
            println("您确定输入是三个参数吗。您确定是三个空格分割的嘛")
        } catch (e: Exception) {
            println("未知错误${e.message}")
        }

        println("再来一次?[Y]")
        val cmd = readLine()
        if (cmd == null || cmd.toLowerCase() != "y") {
            break
        }
    }
    println("感谢您使用我们的计算器")

}

class Operator(op: String) {
    val opFun: (left: Double, right: Double) -> Double

    init {
        opFun = when (op) {
            "+" -> { l, r -> l + r }
            "-" -> { l, r -> l - r }
            "*" -> { l, r -> l * r }
            "/" -> { l, r -> l / r }
            "%" -> { l, r -> l % r }
            else -> {
                throw UnsupportedOperationException(op)
            }
        }
    }

    fun apply(left: Double, right: Double): Double {
        return opFun(left, right)
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值