有没有办法解决这个 kotlin 中的“赋值不是表达式,并且在此上下文中只允许表达式”

is there a way for me to fix the " Assignments are not expressions, and only expressions are allowed in this context" in this kotlin

  1. 这一直显示错误
        fun main() {
            println("Hello, world!")
        }
        fun coinFlip(timesToFlip: Int){
            var heads = 0
            var tails = 0
            fun flip(): Int{
                for(i in 1..timesToFlip){
                    var randomNumbers = (1..2).random()
                    if (randomNumbers = 1){
                         heads += 1
                    } else {
                         tails += 1
                    }
                }
                return tails
            }
        }

我假设您在 randomNumbers = 1 中打算检查 randomNumbers 值是否为 1。在 Kotlin 中,我们使用 == 运算符检查相等性。 = 是赋值运算符。所以你需要将这一行替换为:

if (randomNumbers == 1) {

您可以在此处找到完整的 Kotlin 运算符列表:https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols