那么下面是第一道練習(xí)題:
When it comes to basic operations, it's a good thing for this to become
automatic, something less to think about while you are trying to figure
out the solution to your actual programming challenge. There is only one
road to automaticity, and that's practice. The following are some
starting points for playing in REPL with the operations and concepts
introduced in this lesson.
這是一道十分簡(jiǎn)單的算術(shù)題:
官方答案是:2.plus(71).plus(233).minus(13).div(30).plus(1)
第二題:
Create a String variable rainbowColor, set its color value, then change it.
Create a variable blackColor whose value cannot be changed once assigned. Try changing it anyway.
這道題主要讓大家鞏固一下變量和常量梁呈,前者可以修改,后者不可修改
官方答案是:
var rainbowColor ="red"
rainbowColor ="blue"
val blackColor ="black"
blackColor ="white"http:// Error
第三題:
Try to setrainbowColortonull. Declare two variables,greenColorandblueColor. Use two different ways of setting them tonull.
這是一道關(guān)于null的問題摘刑,知識(shí)點(diǎn)是进宝?這個(gè)符號(hào)
官方答案是:
var greenColor =null
var blueColor: Int? =null
第四題:
Create a list with two elements that are null; do it in two different ways.
Next, create a list where the list is null.
這道題主要是練習(xí)之前講過的空list,包括列表可以為null枷恕,列表元素可以為null
官方答案是:
listOf(null,null)
[null,null]
var list: List<Int?> = listOf(null, null)
var list2:List<Int>? =null
第五題:
Create a nullable integer variable callednullTest, and set it tonull. Use a null-check that increases the value by one if it's not null, otherwise returns 0, and prints the result.
這道題想考察我們對(duì)于null類型的檢查党晋,要求我們使用Elvis operator.,也就是徐块? 未玻?:
官方答案是:
println(nullTest2?.inc()?:0)