簡體   English   中英

有沒有辦法解決這個 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM