How not to repeat math operations two times in calculator app?

后端 未结 1 1413
醉酒成梦
醉酒成梦 2021-01-23 12:43

I have made a simple calculator in "Kotlin" using an android studio the problem I got and I don\'t have a way to fix it is how not to repeat the math operations after

相关标签:
1条回答
  • 2021-01-23 12:59

    If You don't want to add two operators next to each other You have to check if last character is the operator. It will look something like this:

    fun appendOnExpresstion(string: String, canClear: Boolean)
    {
        if (tvResult.text.isNotEmpty())
        {
            tvExpression.text = ""
        }
        if (canClear)
        {
            tvResult.text = ""
            tvExpression.append(string)
        }
        else
        {
            if (tvExpression.text.lastOrNull() !in arrayOf('+', '-', '*', '/')) // You are adding operator so You have to check if last char is oparetor
            {
                tvExpression.append(tvResult.text)
                tvExpression.append(string)
            }
            tvResult.text = ""
        }
    }
    
    0 讨论(0)
提交回复
热议问题