Android 如何创建到数字之间的操作?
在第一次尝试使用kotlin创建操作代码时,我的意思是当在终端中运行代码并输入第一个数字Android 如何创建到数字之间的操作?,android,kotlin,Android,Kotlin,在第一次尝试使用kotlin创建操作代码时,我的意思是当在终端中运行代码并输入第一个数字123456时,当我按下+操作符时,代码将其作为字符串选项读取,它将变为123456+ 底线是:我想创建一个运算符kotlin code可以计算两个数字,当我按+或-或/或*时,数字1的行必须是干净的,才能输入数字2进行计算,从而输入数字3、4和5 这是我的代码: fun operation(arr: ArrayList<String?>?) { val joo = readLine()
123456
时,当我按下+
操作符时,代码将其作为字符串选项读取,它将变为123456+
底线是:我想创建一个运算符kotlin code可以计算两个数字,当我按+
或-
或/
或*
时,数字1的行必须是干净的,才能输入数字2进行计算,从而输入数字3、4和5
这是我的代码:
fun operation(arr: ArrayList<String?>?) {
val joo = readLine()!!
val loo = joo.removeSuffix("+")
var soo = loo.toInt()
arr!!.add(soo.toString())
val voo = joo.lastIndex
when(voo.toString()){
"+" -> arr.last()!!.plus(soo)
}
println("${soo}")
operation(arr)
}
fun操作(arr:ArrayList?){
val joo=readLine()!!
val loo=joo.removeSuffix(“+”)
var soo=loo.toInt()
arr!!.add(soo.toString())
val voo=joo.lastIndex
当(voo.toString()){
“+”->arr.last()!!.plus(soo)
}
println(“${soo}”)
操作(arr)
}
今天早上我有时间。对某人来说,这可能是一个很好的问候世界,所以就这样吧
要从中读取输入并建立状态,您需要:
- 从用户处读取每个新参数的循环
- 一种跟踪已读输入的方法。这实际上是一个小型状态机。我使用
和enum
表达式实现了我的when
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()
}
}
}
}
}
非常感谢你的回答,但实际上这并不完全是我需要的,看,当我添加第一个数字时,我应该按
Enter
键来添加操作员键,这正是我想放弃Enter
键的问题,意思是当按下某个操作员键时+
或-
Enter
键将自动关闭rk.@YounTivoli在本例中,您不需要“readline”语义。因为Enter实际上是一种划界线条的东西。您需要实现一个更为增量的流式读卡器,并对输入的每个新键进行求值。