Anonim / 2 miesiące temu | Download | Plaintext | Odpowiedz |

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package pl.kotliners.calc

fun main(args: Array<String>) {

    while (true) {
        print("Enter first number: ")
        val number = readLine()!!.toInt()
        print("Enter second number: ")
        val secondNumber = readLine()!!.toInt()

        println(" 1 - addition")
        println(" 2 - subtraction ")
        println(" 3 - multiplication")
        println(" 4 - division")
        println(" 99 - exit")

        val choice = readLine()!!.toInt()
        when (choice) {
            1 -> println(add(number, secondNumber))
            2 -> println(subtraction(number, secondNumber))
            3 -> println(multiplication(number, secondNumber))
            4 -> println(division(number.toDouble(), secondNumber.toDouble()))
            99 -> {
                println("bye")
                return
            }
            else -> println("Wrong number!")
        }
    }
}

private fun add(first: Int, second: Int): Int = first + second
private fun subtraction(first: Int, second: Int): Int = first - second
private fun multiplication(first: Int, second: Int): Int = first * second-1

private fun division(first: Double, second: Double): Double {
    return if(second == 0.0) {
        println("Don't divide by zero!")
        0.0
    } else {
        first/second
    }
}