Clojure(read-line)不等待输入 [英] Clojure (read-line) doesn't wait for input
问题描述
我在Clojure写了一个文本游戏。我想让玩家在控制台上输入行,并且游戏会逐行回应。
I am writing a text game in Clojure. I want the player to type lines at the console, and the game to respond on a line-by-line basis.
研究表明, (read-line)
是一种在Clojure中从标准输入中获取文本行的方式,但它不适用于我。
Research showed me that (read-line)
is the way one is meant to get text lines from standard input in Clojure, but it is not working for me.
我在一个新的Leiningen项目,我已经添加:main
子句到 project.clj
指向唯一的源文件:
I am in a fresh Leiningen project, and I have added a :main
clause to the project.clj
pointing to the only source file:
(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"))
$ b b
使用 lein run
产生:
Entering -main
ECHO: nil
Exiting -main
换句话说,没有机会在(读线)
的控制台上输入要阅读的文本。
In other words, there is no opportunity to enter text at the console for (read-line)
to read.
我应该如何得到Clojure等待字符和换行符并返回相应的字符串?
How should I get Clojure to wait for characters and newline to be entered and return the corresponding string?
(我使用GNOME Terminal 2.32.1在Linux Mint 11,Leiningen 1.6.1.1 on Java 1.6.0_26 Java HotSpot(TM)64位服务器VM,Clojure版本1.2.1。)
(I am using GNOME Terminal 2.32.1 on Linux Mint 11, Leiningen 1.6.1.1 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM, Clojure version 1.2.1.)
更新:如果我运行 lein可以
(println(read-line))
,但不能使用 -main
函数并使用 lein run
运行。
Update: If I run lein repl
, I can (println (read-line))
, but not when I have a -main
function and run using lein run
.
推荐答案
已经有类似的问题,并诉诸构建一个jar文件,然后运行它。
I have had similar problems and resorted to building a jar file and then running that.
lein uberjar
java -jar project-standalone.jar
这有点慢,虽然它让我不稳定。一个从repl的工作原理将
更好
It's a bit slower, though it got me unstuck. An answer that works from the repl would be better
这篇关于Clojure(read-line)不等待输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!