使用 while 循环 (Kotlin) 从 1000 到 0 产生以 10 计数的输出

Produce output, that counts by 10, from 1000 to 0 using a while loop (Kotlin)

标题不言自明。我知道如何创建一个从 1000 到 0 的 while 循环倒计时,即倒计时一:

fun main(args: Array<String>) {
  var i = 1000
  while (i > 0) {
    println(i) 
    i--
  }
}

但是,我需要倒数十:1000、990、980。无论出于何种原因,我都在努力解决这个问题。愿意助我一臂之力吗?

(求解释,我不只是要答案,我要的是明白!谢谢大家!)

如果递减区间为10,你的代码i--需要修改为i -= 10

i--等价于i = i -1,原来减1,不符合你的要求


fun main() {
    var i = 1000
    while (i >= 0) {
        println(i)
        i -= 10
    }
}

在while循环中,只需将i--改为i -=10即可。 但这使用 for 循环更直接。

for(i in 1000 downTo 0 step 10) {
    println(i)
}