【发布时间】:2021-08-21 15:56:04
【问题描述】:
我不知道为什么它不起作用.. 我不能在类或“playerPosition”函数中使用 if 或 when 函数..
class Player(val name:String, val nachname: String, val nr:Int) {
var position = mutableListOf<Int>(0,1,2,3)
var playerPosi = Int
fun playerPosition(){
if(playerPosi < 3 ){
}
}
}
接下来的问题是,如何在主函数中使用类中的函数? 可能是这样的
class Player(val name:String, val nachname: String, val nr:Int, var playerPosi : Int) {
var position = mutableListOf<Int>(0,1,2,3)
fun playerPosition(){
if(playerPosi > 3 ){
println("Diese Position gibt es nicht")
}
}
}
主要功能
fun main(){
val team2 = mutableListOf<Player>()
team2.add(Player("Berkan", "Kutlu", 10,4))
val spieler = Player("Wesley","Sneijder",10,4)
playerPosition()
println("${team2.first().playerPosi}")
}
通知 我试图将 playerPosi 的最大 Int 设置为 3。因为进攻、中场、防守和守门员 如果我有从 0 到 3 的数字,我会尝试将 0 识别为 Keeper 1 作为 Defense 等等。
感谢您对我的耐心:D
【问题讨论】:
-
我不禁注意到这是您的第二个问题,您似乎只是缺乏语言语法的基础知识。你看过the official Kotlin documentation吗?它充满了可以帮助您学习基础知识的资源,请查看Basic Syntax、Kotlin by example 和Koans
标签: function class kotlin if-statement