Kotlin 条件分支

2018-08-22  本文已影响3人  wanTag
简单分支
  text_show.text = if (is_odd == true) {
            "凉风有信"
        } else {
            "秋月无边"
        }
多路分支
 private var count: Int = 0

 btn_set_for.setOnClickListener { v ->
            when (count) {
                0 -> text_multi_show.text = "凉风有信"
                1 -> text_multi_show.text = "秋月无边"
                else -> text_multi_show.text = "好诗"
            }
        }
 private var count: Int = 0

 btn_multi.setOnClickListener { v ->
            text_multi_show.text = when (count) {
                0 -> "好好学习"
                1 -> "天天向上"
                else -> "好诗"
            }

        }
//引入变量判断
private var odd: Int = 0
private var even: Int = 1

btn_variable.setOnClickListener { v ->
     text_multi_show.text = when (count) {
            odd -> "好好学习"
            even -> "天天向上"
            else -> "好诗"
       }

 }

上一篇 下一篇

猜你喜欢

热点阅读