例如在 C 中你可以编写
for (int i = 0; i < 10 ; i++) {
if (i == 2) i += 1
// do stuff
}
由于 Scala 使用范围,我们将如何修改迭代器?
最佳答案
你可以使用 guard :
for (i <- 0 to 10; if i != 2) println(i)
这将打印从 0 到 10 的数字,不包括 2。
你可以看到这段代码在运行中,并且可以玩弄它here在 Scastie 上。
关于scala - 如何在Scala的for循环中跳过迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69481372/