Function 未解析的引用:计数错误(Kotlin函数)
我刚开始学习kotlin,我正在kotlin创建的操场上玩耍。在我进行演示练习时,我发现了这些错误。 “未解析的引用:计数” “未解析引用:计数”Function 未解析的引用:计数错误(Kotlin函数),function,kotlin,Function,Kotlin,我刚开始学习kotlin,我正在kotlin创建的操场上玩耍。在我进行演示练习时,我发现了这些错误。 “未解析的引用:计数” “未解析引用:计数” 我不明白为什么,因为我完全模仿了网站上示例中的演示 fun generateAnswerMachine(countThreshold: Int): String { val answerMachine = if (count < countThreshold) { "I have the answer." } els
我不明白为什么,因为我完全模仿了网站上示例中的演示
fun generateAnswerMachine(countThreshold: Int): String {
val answerMachine = if (count < countThreshold) {
"I have the answer."
} else {
"The andswer eludes me"
}
return answerMachine
}
val answerString = generateAnswerMachine(42)
在函数中使用它之前,您必须定义
count
,比如var count=…
谢谢您的回复!它成功了,但我还有一个问题。在我编写的第一个函数中,我想我可以在其中正确定义var count,因为行看起来是有序的。但是如何在第二个函数中定义var count呢?(我发现它们是可以互换的)我不知道您想要实现什么,但是您也可以在函数的参数中添加count,比如:fun generateAnswerString(countThreshold:Int,count:Int):String
fun generateAnswerString(countThreshold: Int): String = if (count > countThreshold) {
"I have the answer"
} else {
"The answer eludes me"
}