kotlin - Kotlin readLine 在基本程序中给出错误,其中输入来自用户,并且显示为输出
问题描述
以下代码:
fun main(args: Array<String>) {
print("Write anything here: ")
val enteredString = readLine()
println("You have entered this: $enteredString")
}
在 KotlinPlayground 中给出以下错误:
Write anything here: You have entered this: null
在这里,用户没有机会输入。在执行初始打印语句后,编译器不会等待用户输入并跳到下一个打印语句。为什么会这样?我在其他几个在线 Kotlin 编译器中尝试过同样的方法,但我得到了同样的错误。
解决方案
没有错误。readLine
只是返回null
(因为 Kotlin Playground 没有可供读取的控制台),并且按预期打印。
例如,在https://ideone.com/上,您可以说出要使用什么输入,它会打印该行(尽管它的 Kotlin 版本已经很老了)。
推荐阅读
- python - 链表:通过在到达其中一个链表的末尾后交换它们的指针来查找 2 个链表的交集
- java - 当时间大于零时,不能在 UI 线程上调用 Await
- r - 读取文件文件夹的函数返回“文件错误(con,“r”):无法打开连接...”
- scala - 将 F[A] 转换为 Future[A]
- android - 将工具栏文本更改为样式中的所有大写字母后,Android 搜索小部件崩溃
- c# - 在不创建实例的情况下获取方法的属性
- python - 如何初始化具有共享状态的python多处理工作者池?
- java - 如果代理关闭,则无法创建 Bean
- python - Python Kivy - 将 GridLayout 添加到 BoxLayout
- c# - 在 Firebase REST API 上放置 JSON 数据时出现 BadRequest