2、Kotlin基础(练习题)

那么下面是第一道练习题:

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.

这是一道十分简单的算术题:

官方答案是: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"// Error

第三题:

Try to setrainbowColortonull. Declare two variables,greenColorandblueColor. Use two different ways of setting them tonull.

这是一道关于null的问题,知识点是?这个符号

官方答案是:

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.

这道题主要是练习之前讲过的空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.

这道题想考察我们对于null类型的检查,要求我们使用Elvis operator.,也就是? ?:

官方答案是:

println(nullTest2?.inc()?:0)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,129评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,140评论 0 13
  • 毫无疑问,对这样一部香港警匪片,我们期待很久。 老杜这几年杀向内地,《盲探》,《单身男女2》,《华丽上班族》,都是...
    Sir电影阅读 8,017评论 3 17
  • 初二的那年是我最痛苦最无助的一年,每天都要被同学欺负,记得那时候我的座位被同学挤的让自己都出不来站不起来,那时的感...
    丰盛的喜儿阅读 1,410评论 0 0
  • 黄土高原上的山圪崂崂上有个小村落叫无人堰,无人堰住着两户人家,两户人家两个姓氏,听卢富贵爹说他家祖上是他老...
    1c8950b7cb1d阅读 3,342评论 2 2