如何创建数字之间的运算?

时间:2019-04-12 17:26:53

标签: android kotlin

首先,我尝试在kotlin中创建操作代码,这是指在终端中运行代码并按123456操作符时输入第一个数字+,代码将其读取为字符串选项,并且将成为123456+

最重要的是:我要创建运算符kotlin代码可以计算两个数字,并且当我按+-/或{{ 1}}的第一行必须是干净的才能输入第二个进行计算,因此要输入第三个,第四个和第五个。

这是我的代码:

*

1 个答案:

答案 0 :(得分:1)

今天早上我有一些时间。对于某人来说,这可能是一个很好的问候世界,所以去吧。

要读取输入并从中建立状态,您将需要:

  • 从用户读取每个新参数的循环
  • 一种跟踪已读输入的方法。这实际上是一个小状态机。我已经使用enumwhen表达式实现了我的工作。

这就是(可能不完全是您要寻找的,但是应该让您了解可能的结构:

import java.util.*

// This gives us the states that our state machine can be in.
enum class State {
    WANT_FIRST_OPERAND,
    WANT_SECOND_OPERAND,
    WANT_OPERATOR
}

fun main() {
    val scanner = Scanner(System.`in`)
    var state = State.WANT_FIRST_OPERAND
    println("Ready to do some maths!")
    var firstOperand = 0.0
    var secondOperand: Double
    var operator = ""
    // This loop will keep asking the user for input and progress through the states of the state machine.
    loop@ while (true) {
        // This when block encapsulates the logic for each state of our state machine.
        when (state) {
            State.WANT_FIRST_OPERAND -> {
                println("Give me your first operand.")
                try {
                    firstOperand = scanner.nextDouble()
                    state = State.WANT_OPERATOR
                } catch (e: Exception) {
                    println("Sorry, that did not work, did you give me a number?")
                    scanner.nextLine()
                }
            }
            State.WANT_OPERATOR -> {
                println("Give me the operator. (+, -, /, *)")
                try {
                    operator = scanner.next("[-*+/]+").trim()
                    state = State.WANT_SECOND_OPERAND
                } catch (e: Exception) {
                    println("Sorry, that did not work.")
                    scanner.nextLine()
                }
            }
            State.WANT_SECOND_OPERAND -> {
                println("Give me your second operand.")
                try {
                    secondOperand = scanner.nextDouble()
                    val answer = when(operator){
                        "+" -> firstOperand + secondOperand
                        "-" -> firstOperand - secondOperand
                        "/" -> firstOperand / secondOperand // You want to do something if second operand is 0 here
                        "*" -> firstOperand * secondOperand
                        else -> {
                            println("Hmmm, something went wrong there I don't know $operator, try again")
                            state = State.WANT_OPERATOR
                            continue@loop
                        }
                    }
                    println("The Answer is $answer, lets do another one!")
                    state = State.WANT_FIRST_OPERAND
                } catch (e: Exception) {
                    println("Sorry, that did not work, did you give me a number?")
                    scanner.nextLine()
                }
            }
        }
    }
}