Kotlin 循环分支

2018-08-22  本文已影响0人  wanTag
遍历循环
    private val poemArray: Array<String> = arrayOf("一去二三里", "烟村四五家", "亭台六七座", "八九十枝花")

    private fun ForEachTest() {
        btn_set_for.setOnClickListener { v ->
            var poem: String = ""
            for (i in poemArray.indices) {
                if (i % 2 == 0) {
                   poem = "$poem${poemArray[i]}, \n"
                }else{
                    poem = "$poem${poemArray[i]}. \n"
                }
            }
            text_show.text = poem
        }

    }

条件循环
      private val poemArray: Array<String> = arrayOf("一去二三里", "烟村四五家", "亭台六七座", "八九十枝花")

       btn_set_for.setOnClickListener { v ->
            var poem: String = ""
            var i: Int = 0
            while (i < poemArray.size) {
                if (i % 2 == 0) {
                    poem = "$poem${poemArray[i]}, \n"
                } else {
                    poem = "$poem${poemArray[i]}. \n"
                }
                i++
            }

            poem = "${poem} \n 该诗歌共${i}句"
            text_show.text = poem
        }
上一篇下一篇

猜你喜欢

热点阅读