赞
踩
- fun main() {
- println("----pleass pass biaodashi ----")
- val input:String? = readLine()
- try {
- input?.let{
- val ret = caculate(it)
- println("${input} = ${ret}")
- println("are you y/n also use?")
- val cmd:String? = readLine()
- cmd?.let {
- if (it.equals("n")){
- System.exit(-1)
- }else{
- return main()
- }
- }
- }
- }catch (ex:Exception){
- ex.printStackTrace()
- }
- }
-
- fun caculate(input:String): String {
- if (input.contains("+")) {
- var nums = input.trim().split("+")
- return operate(nums[0].toDouble(),nums[1].toDouble(),"+").toString()
- }else if (input.contains("-")){
- var nums = input.trim().split("-")
- return operate(nums[0].toDouble(),nums[1].toDouble(),"-").toString()
- }else if (input.contains("/")){
- var nums = input.trim().split("/")
- return operate(nums[0].toDouble(),nums[1].toDouble(),"/").toString()
- }else if (input.contains("*")){
- var nums = input.trim().split("*")
- return operate(nums[0].toDouble(),nums[1].toDouble(),"*").toString()
- }else {
- return "you input biaodashi error"
- }
- }
- fun operate(num_1:Double,num_2: Double,symbol:String):Double{
- return when(symbol){
- "+" -> num_1 + num_2
- "-" -> num_1 - num_2
- "*" -> num_1 * num_2
- "/" -> num_1 / num_2
- else -> 0.0
- }
- }
代码实现效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。