![](/img/trans.png)
[英]Assignments are not expressions and only expressions are allowed in this context - 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
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.