学习kotlin第一天,kotlin方法、kotlin条件表达式、kotlin循环语句和when表达式

经过长时间的断更后,从今天开始继续更新和学习。

今天学习了kotlin方法、kotlin条件表达式、kotlin循环语句和when表达式。一一记录。
/**

  • 有参有返回方法1
    */
    fun sum(a: String,b: String) = a + b

/**

  • 有参数有返回方式2
    */
    fun sum1(a:Int,b:Int): Int{
    println(“这是一个有参数有返回的方法书写方式2”)
    return a+b
    }

/**

  • 有参数无返回值方式1
    */
    fun sum2(a:Int,b: Int): Unit{
    println(“这是一个有参数无返回的方法书写方式1,参数1=0”+a+"/参数2="+b)
    }

/**

  • 有参数无返回值方式2
    /
    fun sum3(a:Int,b: Int){
    println(“这是一个有参数无返回的方法书写方式2,参数1=0”+a+"/参数2="+b)
    }
    /
    *
  • 有参数无返回值方式1
    */
    fun sum4(){
    println(“这是一个无参方法”)
    }

/**

  • 条件表达式
    */
    fun maxOf(a: Int,b: Int):Int{
    if (a > b){
    return a
    }else{
    return b
    }
    }

/**

  • 判断数据类型
    */
    fun checkType(a:Any){
    if (a is String){
    println(“该数据是字符串类型”)
    }else if (a is Int){
    println(“该数据是整数类型”)
    }else if (a is Double){
    println(“该数据是小数类型”)
    }else if (a is Float){
    println(“该数据是浮点类型”)
    }else{
    println(“该数据是其他的类型”)
    }
    }

/**

  • for 循环
    */

fun forErch(){
val lists = listOf(“110”,“200”,“啥也不是”)
for (l in lists){
println(“for循环集合元素=”+l)
}
}

/**

  • while 循环
    */
    fun whileErch(){
    val lists = listOf(“元素1”,“元素2”,“元素3”)
    var index = 0
    while (index < lists.size){
    // println(“item at $index is ${lists[index]}”)
    println(lists[index])
    index++
    }
    }

/**

  • 类型转换
    */
    fun changeType(a: Any):Int?{

    if (a is String){
    return a.length
    }
    return null
    }

/**

  • when表达式
    */
    fun whenType(a: Any){
    when(a){
    1 -> println(“参数是整数类型”)
    “1” -> println(“参数是字符串类型”)
    1.0 -> println(“参数是小数类型”)
    1.00f -> println(“参数是浮点数数类型”)
    true -> println(“参数是布尔类型”)
    else -> println(“参数是未知类型”)
    }
    }

明天学习kotlin集合,明日继续更新。

发布了75 篇原创文章 · 获赞 81 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Naide_S/article/details/104665114
今日推荐