我正在 Clojure 中编写文本游戏。我希望玩家在控制台上输入行,而游戏则逐行响应。
研究表明 (read-line)
是一种从 Clojure 中的标准输入获取文本行的方式,但它对我不起作用。
我在一个新的 Leiningen 项目中,我添加了一个 :main
条款给 project.clj
指向唯一的源文件:
(ns textgame.core)
(defn -main [& args]
(println "Entering -main")
; (flush) ;makes no difference if flush are commented out
(let [input (read-line)]
(println "ECHO:" input))
; (flush)
(println "Exiting -main"))
使用
lein run
产量:Entering -main
ECHO: nil
Exiting -main
换句话说,没有机会在控制台输入
(read-line)
的文本。阅读。我应该如何让 Clojure 等待输入字符和换行符并返回相应的字符串?
(我在 Linux Mint 11 上使用 GNOME Terminal 2.32.1,在 Java 1.6.0_26 Java HotSpot(TM) 64 位服务器 VM 上使用 Leiningen 1.6.1.1,Clojure 版本 1.2.1。)
更新:如果我运行
lein repl
,我可以(println (read-line))
,但不是当我有 -main
函数并使用 lein run
运行.
最佳答案
试试“lein 蹦床跑”。见 http://groups.google.com/group/leiningen/browse_thread/thread/a07a7f10edb77c9b更多详情也来自 https://github.com/technomancy/leiningen :
Q: I don't have access to stdin inside my project.
A: There's a problem in the library that Leiningen uses to spawn new processes that blocks access to console input. This means that functions like read-line will not work as expected in most contexts, though the repl task necessarily includes a workaround. You can also use the trampoline task to launch your project's JVM after Leiningen's has exited rather than launching it as a subprocess.
关于Clojure(读行)不等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7707558/