美文网首页
Kotlin 循环分支

Kotlin 循环分支

作者: wanTag | 来源:发表于2018-08-22 14:38 被阅读0次
    遍历循环
        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
            }
    

    相关文章

      网友评论

          本文标题:Kotlin 循环分支

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