[Android App 開發筆記] #從零開始 #D04: Kotlin Functions, Part-1

[Android App 開發筆記] #從零開始 #D04: Kotlin Functions, Part-1

# Android App 開發

# Reference: Kotlin Bootcamp for Programmers (Link), Lesson 3: Kotlin Functions (Link)

# Kotlin docs (所有不會的程式碼都可以看這裡: Link)


[從 main ( ) function 開始]

***注意 (version 17.) new project 是 Java 裡面的 Kotlin/JVM***

開始寫 CODE 囉: Hello, world!

  • 左邊的 src 右鍵新增 new Kotlin Class / File


  • argument: 參數
  • the Kotlin main() function specifies the entry point for execution.
  • Any command line arguments are passes as an array of strings
fun main(args: Array<String>){println("Hellow, world!")}

Lesson 04, Quiz 01

# call function from the main function
# 今天星期幾的函數

fun main(args: Array<String>){
    println("Hello, world!")
    dayOfWeek()
}

fun dayOfWeek(){
    println("Wht day is it tdy")
    val day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
    println("the day is" + " " + day)
    println("today is")
    when (day){
        1 -> println ("Sun.")
        2 -> println ("Mon.")
        3 -> println ("Tue.")
        4 -> println ("Wed.")
        5 -> println ("Thur.")
        6 -> println ("Fri.")
        7 -> println ("Sat.")
    }


[把參數放到 Main 函數裡面]

Pass arguments to main()
  • run → Edit Configurations → 把參數輸入 "Program arguments" 裡面

Change the code to use a string template
  • change the greeting message to use the first argument passed into the program: args[0]
  • $ → 告訴那個 string 是 variable or expression

fun main(args: Array<String>){
println("Hello, ${args[0])
}

**********************************
在 Kotlin 裡面幾乎 everything is an expression and "has a value"

**********************************


💭 Murmur time 💭 

也學了一些不同的 Language, 真的是一開始最痛苦, 這個也不例外

By Mark, 馬克

💬 E-mail: mata.assembled@gmail.com







0 Comments