gpt4 book ai didi

java - 如何在 Kotlin 计算器应用程序中重置变量

转载 作者:行者123 更新时间:2023-12-01 19:12:41 24 4
gpt4 key购买 nike

所以我刚刚开始使用 Kotlin,所以我决定制作一个简单的计算器应用程序。计算器可以工作,但在尝试新的计算时,即使在清除屏幕后,它显然仍然可以使用以前的值。例如,2 + 2 将等于 4。如果尝试计算新数字,我只需按 CLS 按钮,屏幕就会变成空白。然而,问题是如果我想计算 2+3 这样的东西,它不会等于 5,而是等于 9。它会添加到之前的结果中。因此,即使在清除后,占位符仍保留先前计算的值。

我的清除屏幕监听器使用 clear() 从屏幕上删除数字,但不清除占位符。这是我的代码块


val ClearButtonListener = View.OnClickListener { v ->
val b = v as Button
if (newNumber != null) {
newNumber.getText().clear()
result.getText().clear()
}
}

newNumber 和 result 是 EditText 小部件,并使用 Lateinit 进行声明。所以我的问题是如何将这些小部件重置回 null,而不是保留以前的值?

我的整个代码:


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.NumberFormatException

class MainActivity : AppCompatActivity() {
private lateinit var result: EditText
private lateinit var newNumber: EditText
private val displayOperation by lazy(LazyThreadSafetyMode.NONE) { findViewById<TextView>(R.id.operation) }

private var operand1: Double? = null
private var pendingOperation = "="

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

result = findViewById(R.id.result)
newNumber = findViewById(R.id.newNumber)
//Data input buttons
val button0: Button = findViewById(R.id.button0)
val button1: Button = findViewById(R.id.button1)
val button2: Button = findViewById(R.id.button2)
val button3: Button = findViewById(R.id.button3)
val button4: Button = findViewById(R.id.button4)
val button5: Button = findViewById(R.id.button5)
val button6: Button = findViewById(R.id.button6)
val button7: Button = findViewById(R.id.button7)
val button8: Button = findViewById(R.id.button8)
val button9: Button = findViewById(R.id.button9)
val buttonDot: Button = findViewById(R.id.buttonDecimal)

val clearButton: Button = findViewById(R.id.clear)

// operation buttons
val Buttonequals: Button = findViewById(R.id.equal)
val ButtonPlus: Button = findViewById(R.id.add)
val ButtonMinus: Button = findViewById(R.id.Subtract)
val ButtonMultiply: Button = findViewById(R.id.Multiply)
val ButtonDivide: Button = findViewById(R.id.Divide)

val listener = View.OnClickListener { v ->
val b = v as Button
newNumber.append(b.text)
}

//Clear button
val ClearButtonListener = View.OnClickListener { v ->
val b = v as Button
if (newNumber != null) {
newNumber.getText().clear()
result.getText().clear()
}
}
button0.setOnClickListener(listener)
button1.setOnClickListener(listener)
button2.setOnClickListener(listener)
button3.setOnClickListener(listener)
button4.setOnClickListener(listener)
button5.setOnClickListener(listener)
button6.setOnClickListener(listener)
button7.setOnClickListener(listener)
button8.setOnClickListener(listener)
button9.setOnClickListener(listener)
buttonDot.setOnClickListener(listener)

clearButton.setOnClickListener(ClearButtonListener)

val opListener = View.OnClickListener { v ->
val op = (v as Button).text.toString()
try {
val value = newNumber.text.toString().toDouble()
performOperation(value, op)
} catch (e: NumberFormatException) {
newNumber.setText("")
}

pendingOperation = op
displayOperation.text = pendingOperation
}

Buttonequals.setOnClickListener(opListener)
ButtonPlus.setOnClickListener(opListener)
ButtonMinus.setOnClickListener(opListener)
ButtonMultiply.setOnClickListener(opListener)
ButtonDivide.setOnClickListener(opListener)
}
private fun performOperation(value: Double, operation: String) {
if (operand1 == null) {
operand1 = value
} else {


if (pendingOperation == "=") {
pendingOperation = operation
}

when (pendingOperation) {
"=" -> operand1 = value
"+" -> operand1 = operand1!! + value
"-" -> operand1 = operand1!! - value
"*" -> operand1 = operand1!! * value
"/" -> if (value == 0.0) {
operand1 = Double.NaN
} else {
operand1 = operand1!! / value
}
}
}
result.setText(operand1.toString())
newNumber.setText("")
}
}

最佳答案

您从未重置operand1。因此,在获得第一个结果后,不会到达 performOperation 的第二行。然后,operand1 仍保留您在 when 子句中为 = 设置的最后一个值。在 result.setText(operand1.toString()) 之后,调用

if (pendingOperation == "=") {
operand1 = null
}

这应该可以解决问题。

关于java - 如何在 Kotlin 计算器应用程序中重置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59458340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com