如何通过println(i for i in 1..10)缩短代码,就像Kotlin中的Python一样

Trầ*_*Tâm 0 iterator kotlin

我现在正在学习Kotlin.

当我阅读Kotlin文档时,它写道我有ranges像Python 这样的技术(例如:1..10).但是,我尝试下面的代码,但失败了.

>>> print(i for i in 1..10)
...
error: expecting ')'
print(i for i in 1..10)
        ^
error: unexpected tokens (use ';' to separate expressions on the same line)
print(i for i in 1..10)
                    ^
Run Code Online (Sandbox Code Playgroud)

有没有办法通过使用i for i in 1..10类似Python 来缩短此代码.

s1m*_*nw1 5

您的代码在Kotlin中无效.您可以使用该forEach功能:

(1..10).forEach(::println)
Run Code Online (Sandbox Code Playgroud)