在Kotlin中使用多变量进行循环 [英] For Loop with Multivariable in Kotlin
本文介绍了在Kotlin中使用多变量进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅使用一个for循环在kotlin中执行此Java代码?
How can I do this java code in kotlin using just one for loop?
for(int i=0, j=0; i < 6 && j < 6; i++, j+=2) {
// code here
}
推荐答案
无法迭代多个变量.在这种情况下,最简单的操作是:
There is no way to iterate over multiple variables. In this case, the easiest thing you can do is:
for (i in 0..3) {
val j = i * 2
}
在更一般的情况下,您可以将其重写为while
循环:
In a more general case, you can rewrite this as a while
loop:
var i = 0
var j = 0
while (i < 6 && j < 6) {
// code here
i++
j += 2
}
这篇关于在Kotlin中使用多变量进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文