3、函数

这一期的视频主要内容是Kotlin中的函数
我们从这一期开始需要用到源文件,在IDEA中新建一个Kotlin文件的步骤如下:

  1. 找到项目视图中的src文件夹
  2. 在src文件夹上右键->New->Kotlin File/Class
  3. 填写文件信息并确认

要运行Kotlin文件,文件中必须要有一个主函数:

fun main(args:Array<String>){
    
}

注意,Kotlin的所有函数都会返回一些东西(即使没有return语句),类似主函数这样的函数会返回一个type unit来代表没有值。type unit就是Kotlin认为的没有值,当一个函数返回type unit,那么就没必要写return

接下来是一个练习(必须要做,以后还会用到):

  1. Create a new Kotlin file.
  2. Copy and paste the main() function from Hello World into the file.
  3. Create a new function, dayOfWeek().
  4. In the body of the function, print "What day is it today?"
  5. Call dayOfWeek() from main().
  6. Run your program.

官方答案是:

fun main(args: Array<String>) {
   dayOfWeek()
}

fun dayOfWeek() {
   println("What day is it today?")
}

下面继续练习:
In the body of the dayOfWeek() function, answer the question by printing the current day of the week.

这里有一系列提示:

  1. You can use Java libraries (java.util) from Kotlin. For example, to get the day of the week:
  2. Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
  3. Type in the code, then press Option + Enter in Mac, or Alt + Enter in Windows, over the red Calendar class to import the library.
  4. Use a when statement to print a string depending on the day. Sunday is the first day of the week.

官方答案是:

fun dayOfWeek() {
   println("What day is it today?")
   val day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
   println( when (day) {
      1 -> "Sunday"
      2 -> "Monday"
      3 -> "Tuesday"
      4 -> "Wednesday"
      5 -> "Thursday"
      6 -> "Friday"
      7 -> "Saturday"
      else -> "Time has stopped"
   })
}

接下来我们要学习的是命令行参数。
首先在IDEA中打开run->edit configuration,在program arguments中填入kotlin,之后确认
然后我们需要在程序中使用这个参数,将main函数修改如下:

fun main(args:Array<String>){
    println("我爱你~${args[0]}")

    val isUnit = println("This is an expression")
    println(isUnit)

    val temperature = 10
    val isHot = if(temperature>50) true else false
    println(isHot)

    val message = "You are ${if(temperature>50) "fried" else "safe"} fish"
    println(message)
}

上面的例子还说明了,在${}中可以使用表达式
那么再做一些练习题~
Create a main() function that takes an argument representing the time in 24-hour format (values between and including 0 -> 23).

In the main() function, check if the time is before midday (<12), then print "Good morning, Kotlin"; otherwise, print "Good night, Kotlin".
注意:
Remember that all main() function arguments are Strings, so you will have to convert this argument to an Int before you can apply the check.
高级一点:
Try to use Kotlin's string templates to do this in 1 line.

正确答案是(包含两种写法):

fun main(args:Array<String>){
    if (args[0].toInt()<12) println("Good morning Kotlin")
    else println("Good night Kotlin")

    // 一行表示
    println("Good ${if (args[0].toInt()<12) "morning" else "night"},Kotlin")
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 每个女人心中都有一个自己勾画的专属王子,几乎就没有人找到一个完完全全的顺自己心意的先生,有的差那么一毫一厘,也...
    最初的遗忘阅读 218评论 0 0
  • 光胖子约小东出去旅游,然后他说,那个小智很少出门,没什么见识,一起带着吧。小东同意。三人一起抵达火车站,光胖子和小...
    成不熟阅读 170评论 0 0
  • 苍天问我志当何,敢教苍天竟无颜。 明月问我志当何,敢教明月照无眠。 我辈少年志凌云,扶风摇上九重天。 莫堕少年真豪...
    羽月阁阅读 251评论 0 1
  • 文/完美的补丁 (我、母亲和小外甥。) (外甥女母子) 今天不只是小朋友的节日,也是每一个老顽童的节日。 我们几代...
    完美的补丁阅读 639评论 0 6