Temps : 60 min.
Diffiulté : *
L'objectifs est de se familiariser avec les bases du langage Kotlin, autour de l'univers de la plage, en 6 étapes :
En Kotlin, une variable est soit mutable soit immuable. De plus, il n'est pas nécéssaire de préciser le type, il est déduit implicitement.
main()
, créez une variable immuable :
val beach = "Bocca"
beach = "Barceloneta"
var beach = "Bocca"
beach
dans la console :
println(beach)
Le code complet est :
fun main() {
var beach = "Bocca"
beach = "Barcelonetta"
println(beach)
}
En Kotlin, tous les types commencent par une majuscule.
Voici les types primitifs nombres :
Double
(64)Float
(32)Long
(64)Int
(32)Short
(16)Byte
(8)main()
, du compilateur en ligne
val lOfBridge1 = 9.99
val lOfBridge2 = 13F
val bridgeLength = lOfBridge1.toFloat() + lOfBridge2
val oneMillion = 1_000_000
println("The length of the bridge is ${bridgeLength.toInt()} and $oneMillion grain of sand, on the $beach beach!")
Le code complet est :
fun main() {
var beach = "Bocca"
beach = "Barcelonetta"
val oneMillion = 1_000_000
val lOfBridge1 = 9.99
val lOfBridge2 = 13F
val bridgeLength = lOfBridge1.toFloat() + lOfBridge2
println("The length of the bridge is ${bridgeLength.toInt()} and $oneMillion grain of sand, on the $beach beach!")
}
Les opérateurs Kotlin indispensables sont similaires à ceux de Java ou tout autre langage de programmation. Les opérateurs sont catégorisés selon :
a++
)a..b
, a in b
)a[i] = b
)a == b
)a < b
)as
)Les opérateurs Kotlin ne sont pas originaux à l'exception
du rang ..
et du in
.
main()
, du compilateur en ligne
val nbTowel = 10
if
suivante :
if (nbTowel in 0..10)
println("There is not so much towel at the beach.")
else
println("The number of towel is not suitable for a beach.")
Le code complet est :
fun main() {
val nbTowel = 10
if (nbTowel in 0..10)
println("There is not so much towel at the beach.")
else
println("The number of towel is not suitable for a beach.")
}
La condition when
en Kotlin est équivalente au switch case
.
main()
, du compilateur en ligne
val temp = 30
var message = "It's cold"
when
suivante :
when (temp) {
in 0..10 -> message = "It's very cold"
in 10..20 -> message = "The temperature is kind of correct"
in 20..30 -> message = "It's hot"
else -> message = "It's very hot"
}
println(message)
Le code complet est :
fun main() {
val temp = 30
var message = "It's cold"
when (temp) {
in 0..10 -> message = "It's very cold"
in 10..20 -> message = "The temperature is kind of correct"
in 20..30 -> message = "It's hot"
else -> message = "It's very hot"
}
println(message)
}
Les différents types de collection en Kotlin sont :
Array
IntArray
List
MutableList
ArrayList
main()
, du compilateur en ligne
val beaches = listOf("Plage des Rochers", "Praia de Carcavelos", "Playa de la Barceloneta")
println(beaches)
val fishes = mutableListOf("tuna", "salmon", "shark")
fishes.remove("shark")
fishes.add("yellowfish")
Le code complet est :
fun main() {
val beaches = listOf("Plage des Rochers", "Praia de Carcavelos", "Playa de la Barceloneta")
println(beaches)
val fishes = mutableListOf("tuna", "salmon", "shark")
println(fishes)
fishes.remove("shark")
println(fishes)
fishes.add("yellowfish")
println(fishes)
}
Les tableaux en Kotlin sont indiqués pour les collections de taille fixe. En effet, même s'il est possible de faire évoluer leur taille, l'espace alloué est fixe.
main()
, du compilateur en ligne
val beachParty = arrayOf("Plage des Rochers", true, 7)
println(java.util.Arrays.toString(beachParty))
val deckArray = intArrayOf(3, 7, 13, 21)
val bridgeArray = intArrayOf(1, 5, 9)
val deckAndBridge = deckArray + bridgeArray
println(deckAndBridge[5])
val deckLabelArray = Array(21) { i -> "Deck nº$i" }
Le code complet est :
fun main() {
val beachParty = arrayOf("Plage des Rochers", true, 7)
println(java.util.Arrays.toString(beachParty))
val deckArray = intArrayOf(3, 7, 13, 21)
println(java.util.Arrays.toString(deckArray))
val bridgeArray = intArrayOf(1, 5, 9)
val deckAndBridge = deckArray + bridgeArray
println(deckAndBridge[5])
val deckLabelArray = Array(21) { i -> "Deck nº$i" }
println(java.util.Arrays.toString(deckLabelArray))
}
En Kotlin, la boucle for
s'utilise de différentes façons.
main()
, du compilateur en lignedeckLabelArray
pour l'afficher différemment :
for (item in deckLabelArray)
println(" #" + item)
deckAndBridge
pour afficher l'indexation :
for ((index, item) in deckAndBridge.withIndex())
println("The number of the deck located at $index is $item ")
Le code complet est :
fun main() {
val deckArray = intArrayOf(3, 7, 13, 21)
val bridgeArray = intArrayOf(1, 5, 9)
val deckAndBridge = deckArray + bridgeArray
val deckLabelArray = Array(9) { i -> "Deck nº$i" }
for (item in deckLabelArray)
println(" #" + item)
for ((index, item) in deckAndBridge.withIndex())
println("The number of the deck located at $index is $item ")
}
main()
, du compilateur en lignefor
pour afficher des nombres :
for (i in 1..5) print("-> nº $i ")
for
pour afficher un compte à rebours :
for (i in 5 downTo 0) print("$i ... ")
for
pour afficher par palier :
for (i in 0..10 step 2) print("|_ $i ")
for
pour afficher l'alphabet :
for (a in 'a'..'z') print("$a ")
Il y a aussi les boucles do while
et repeat
.
main()
, du compilateur en lignewhile do
pour afficher des bulles :
var bubbles = 0
while (bubbles < 5) {
bubbles++
println(". º 0")
}
do while
pour enlever les bulles :
do {
bubbles--
} while (bubbles > 0)
println("$bubbles bubbles in the water\n")
repeat
pour faire des bulles :
repeat(3) {
println("Making bubbles . ººº 0")
}
Le code complet est :
fun main() {
var bubbles = 0
while (bubbles < 5) {
bubbles++
println(". º 0")
}
do {
bubbles--
} while (bubbles > 0)
println("$bubbles bubbles in the water\n")
repeat(3) {
println("Making bubbles . ººº 0")
}
}
Finalement, il a été vu :
var
et immuable val
if else
et when(){ i -> {do} }
for
, do while
et repeat
developer.android.com: Write conditionals in Kotlin
developer.android.com: Kotlin basics
chillcoding.com: Initialiser un tableau en Kotlin