0
fun main() {
    println("This is a calculator")
    println("enter your first number")
    val no1 = readLine()!!
    println("enter your operation")
    val operation1 = readLine()!!
    println("enter your second number")
    val no2 = readLine()!!

    val result = if (operation1 == "*")
        print(no1.toInt() * no2.toInt())
    else if (operation1 == "+")
        print(no1.toInt() + no2.toInt())
    else if (operation1 == "-")
        print(no1.toInt() - no2.toInt())
    else if (operation1 == "/")
        print(no1.toInt() / no2.toInt())
    else
        println("Please Enter just numbers and operations")

}

Whenever I run this, I can use whole numbers fine but the minute the answer is a decimal, it rounds and the if the userinput is a decimal, it comes up with an error.

1 Answers1

1

if you change all of your toInt() into toDouble() it will print out everything with a decimal if it's a whole number or not but it will work correctly.

val result = if (operation1 == "*")
    print(no1.toDouble() * no2.toDouble())
else if (operation1 == "+")
    print(no1.toDouble() + no2.toDouble())
else if (operation1 == "-")
    print(no1.toDouble() - no2.toDouble())
else if (operation1 == "/")
    print(no1.toDouble() / no2.toDouble())
else
    println("Please Enter just numbers and operations")
thatWaz96
  • 33
  • 1
  • 4