Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
637 B
2
Indexable
Never
package com.example.alarmclock.utils

class MathProblem {
    val question: String
    private val answer: Int

    init {

        val num1 = (0..10).random()
        val num2 = (0..10).random()
        val operator = listOf("+", "-", "*", "/").random()


        question = "$num1 $operator $num2 ="
        answer = when (operator) {
            "+" -> num1 + num2
            "-" -> num1 - num2
            "*" -> num1 * num2
            "/" -> num1 / num2
            else -> throw IllegalArgumentException("Invalid operator")
        }
    }

    fun checkAnswer(answer: Int): Boolean {
        return answer == this.answer
    }
}
Leave a Comment