.append in kotlin to showing expected result

Viewed 56

I am building a calculator app, but when I clear the screen, the expression becomes 0. If I click a number then, it is appended to the expression(like, 05 instead of 5). Help me with the logic.

This is my code. 'string' is the button clicked( numbers(0,1,2,3,4,5,6,7,8,9) or operation(+, -, *, /) ) 'Result.text' is the result screen text after calculation. 'Expression.text' is the expression build after clicking buttons. Wrote a condition, if 'Expression.text' is 0, rewrite the text instead of appending the number.

'''

Result.text = ""
if (string == "0" ||string == "1" || string == "2" || string == "3" || string == "4" ||
    string == "5" || string == "6" || string == "7" || string == "8" || string == "9"){
    if(Expression.text == "0") {
       Expression.text = string
    }else {
       Expression.append(string)
    }
}else if (string == "+" || string == "-" || string == "/" || string == "*") {
        while (Expression.text[Expression.text.length - 1] == '+' || 
               Expression.text[Expression.text.length - 1] == '-' ||
               Expression.text[Expression.text.length - 1] == '*' || 
               Expression.text[Expression.text.length - 1] == '/'){

                   Expression.text = Expression.text.dropLast(1)
        }
        Expression.append(string)
}

'''

0 Answers
Related