美文网首页
3、函数

3、函数

作者: 北方素素 | 来源:发表于2018-06-23 09:29 被阅读0次

这一期的视频主要内容是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")
}

相关文章

  • python3 range() 函数和 xrange() 函数

    python3 range 函数 python3 取消了 xrange() 函数,并且和 range() 函数合并...

  • 07 C函数

    1、why函数? 2、函数进阶 3、递归函数

  • python3函数(一)

    python3中可以调用函数和定义函数。 1、调用函数 直接调用python3自带的函数 (1)函数abs(-10...

  • python中删除列表中重复元素

    1使用内置函数 2 unique函数 3 del函数

  • Python_9_函数定义-位置参数-返回值

    1. 函数介绍1.1. 为什么要使用函数1.2. Python 中的函数 2. 函数的基本使用 3. 函数的参数3...

  • 3、函数

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

  • 函数3

    1.回调 函数让不连续的事件处理变得容易起来 下面我们来看一个同步的传统的例子 上述的例子是同步发送的,如果服...

  • 3 - 函数

    函数可以返回多个返回值 函数的值都是值传递 函数可以作为变量的值 函数可以作为参数和返回值 通过上面的这个性质,你...

  • ES6入门之函数的扩展

    函数的扩展分为以下3个部分: 1 为函数参数指定默认值2 函数的 rest 参数3 箭头函数 为函数参数指定默认值...

  • Python_8_Python内建函数-迭代器

    1. 内建函数 1 2. 内建函数 2 3. 内建函数 3 4. 内建函数 4 5. 内建函数 5 6. 内建函数...

网友评论

      本文标题:3、函数

      本文链接:https://www.haomeiwen.com/subject/haekyftx.html