# 2020-04-08 | Kotlin | Bases ###### tags: `kotlin` `android` `gobelins` ## Exemples ### Base ```kotlin fun sayHello(){ println("Hello !") } val result = sayHello() println(result.toString()) fun sum(a: Int, b: Int) { println("Resultat : " + (a + b)) } fun sum1(a: Int, b: Int) :Int = a + b sum(1,2) sum1(1,2) ``` ### Nullable ```kotlin fun testNull() { val nullable: String? = null val length = nullable?.length ?: 0 //oui print(length) //oui print(length + 14) } testNull() ``` ### When ```kotlin fun testWhen(a:Int){ when(a) { 0 -> println("a vaut rien") 1 -> println("a vaut 1") else -> println("a vaut plusieurs") } } testWhen(0) testWhen(1) testWhen(13) enum class Color { RED, BLUE, GREEN, UNKNOWN} fun testWhenEnum(color:Color){ when (color) { Color.BLUE -> println("Du bleu") Color.RED -> println("Du rouge") Color.GREEN -> println("Du vert") Color.UNKNOWN -> println("Du je ne sais pas") } } testWhenEnum(Color.BLUE) fun testWhenBis(a:Int){ when(a) { 0,1,2 -> println("a vaut 0, 1 ou 2") 3,4,5 -> println("a vaut 3, 4 ou 5") else -> println("a vaut plusieurs") } } testWhenBis(4) fun testPlage(a:Int){ when(a) { in 0..10 -> println("de 0 à 10") in 11..20 -> println("de 10 à 20") in 21..50 -> println("de 20 à 50") else -> println("50 ou +") } } testPlage(3) fun testMix(a:Int){ when { a in 0..10 -> println("de 0 à 10") a in 11..20 -> println("de 10 à 20") a == 100 -> println("egal à 100") a > 50 -> println("sup à 50") else -> println("autre") } } testMix(100) ``` ### For ```kotlin fun testFor(){ for (i in 0..10) { println("Valeur de i: $i") } } testFor() ``` ## Exercices ### Ecrire une fonction qui affiche les x premiers nombres ```kotlin fun displayNbPairs(n:Int){ for (i in 0 until n*2 step 2) { println(i) } } fun displayNbImppairs(n:Int){ for (i in 1 until n*2 step 2) { println(i) } } displayNbPairs(10) displayNbImppairs(10) ``` ### Ecrire une fonction affichant les x premiers nombres de la suite Fibonacci ```kotlin fun fobonacci(x:Int){ var next = 0 var variabel1 = 0 var variabel2 = 1 for (i in 0 until x) { if (i <= 1){ next = i } else { next = variabel1 + variabel2 variabel1 = variabel2 variabel2 = next } println(next) } } fobonacci(6) ``` ### Ecrire une fonction qui calculera le factoriel d’un nombre x, utiliser 10 par défaut si aucun nombre n’est spécifié. ```kotlin fun factoriel(x:Int = 10){ var facto:Long = 1 for (i in 1..x) { facto = facto*i } println(facto) } factoriel() factoriel(18) ``` ### Ecrire une fonction qui affiche les x premiers nombres premiers ```kotlin fun displayNbPremiers(x:Int){ val list: ArrayList<Int> = ArrayList() var i = 0 while (list.size < x) { i++ if (testIfPremier(i)){ list.add(i) } } var size = list.size println(list) println("Taille du tableau : $size") } fun testIfPremier(n:Int):Boolean{ var rest = 0 var result = true for(i in 2..n/2) { // le nombre est divisible par lui-meme rest = n%i // si le reste est 0, alors arrete la boucle. Sinon continuer la boucle if(rest == 0) { return false } } return true } displayNbPremiers(30) displayNbPremiers(5) displayNbPremiers(110) ```