I am learning to develop on Android with the Kotlin programming language. I wanted to know if you could help me.
I have a problem when printing a subtraction by console, I show you the code:
print("Escribe le primer valor: ")
val num1 = readLine()!!.toInt()
print("Escribe el segundo valor: ")
val num2 = readLine()!!.toInt()
print( "${num1} - ${num2}" + num1 - num2 ) => el error me lo marca en el segundo menos
thanks for your help
string templates in kotlin can be done through the sign
$
in a variable but to access an object and do operations the expression is used${}
so you should place it as follows:or also
I already found the answer and it is to add parentheses in this way
BEFORE with the error println("${num1} - ${num2} = " + num1 - num2)
NOW with the solution print("${num1} - ${num2} = " + (num1 - num2))
Add the operation of the variables between parentheses, so that it concatenates the string with the value of the operation:
code: