samedi 21 août 2021

Trying to use if function in Kotlin class

I don´t know why it doesn´t work.. I cant use if or when functions in class or in the "playerPosition" function..

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 ){

      }
    }

}

And the next question is, how can I use a function from a class in the main func.? Maybe like this

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")

      }
    }



   }

MAIN FUNCTION

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}")



    }

notice Im trying to set the max Int from playerPosi to 3. Because of Offense, Middfield and Defense and Keeper If Ive the numbers from 0 to 3 I will try to identify 0 as Keeper 1 as Defense and so on.

Thanks for you patience with me :D

Aucun commentaire:

Enregistrer un commentaire