2. Strings
"Hello World"
res0: kotlin.String = Hello World
"Hello "+ "World"
res1: kotlin.String = Hello World
val my_age = 22
val my_height = 183
"I am $my_age years old, and my height is $my_height centimeters or ${my_height/100.0} meters"
res4: kotlin.String = I am 22 years old, and my height is 183 centimeters or 1.83 meters
Practice Time
- Create three String variables for
trout
,haddock
, andsnapper
. - Use a String template to print whether you do or don't like to eat these kinds of fish.
val trout = "trout"
var haddock = "haddock"
var snapper = "snapper"
println("I like to eat $trout and $snapper, but not a big fan of $haddock.")
Practice Time
when
statements in Kotlin are like case
or switch
statements in other languages.
Create a when
statement with three comparisons:
- If the length of the
fishName
is 0, print an error message. - If the length is in the range of 3...12, print "Good fish name".
- If it's anything else, print "OK fish name".
when(fishName.length){
0 -> println("Fish name cannot be empty")
in 3..12 -> println("Good fish name")
else -> println("OK fish name")
}
Comments
Post a Comment