Clojure 中如何获取用户输入?

发布于 2024-07-26 22:39:34 字数 155 浏览 10 评论 0原文

我目前正在学习 clojure,但我想知道如何在 clojure 程序中获取和存储用户输入。 我正在查看 clojure api,发现了一个名为 read-line 的函数,但是我不确定如何使用它(如果它是正确的函数)...

无论如何,如何在 clojure 中获取用户输入?

I'm currently learning clojure, but I was wondering how to get and store user input in a clojure program. I was looking at the clojure api and I found a function called read-line, however I'm not sure how to use it if it's the right function to use...

Anyhow, how do you get user input in clojure ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒时光 2024-08-02 22:39:34

read-line 是正确的函数

(println (read-line))

...... .基本上会回显用户输入:

Clojure 1.0.0-
user=> (println (read-line))
this is my input
this is my input

要在 if 语句中使用它,您可能会使用 let:

(let [yayinput (read-line)]
  (if (= yayinput "1234")
    (println "Correct")
    (println "Wrong")))

希望这足以让您开始,因为这大约是我 Clojure 知识的限制!

read-line is the correct function..

(println (read-line))

..would basically echo the users input:

Clojure 1.0.0-
user=> (println (read-line))
this is my input
this is my input

To use it in an if statement, you'd probably use let:

(let [yayinput (read-line)]
  (if (= yayinput "1234")
    (println "Correct")
    (println "Wrong")))

Hope that's enough to get you started, because that's about the limit of my Clojure knowledge!

清秋悲枫 2024-08-02 22:39:34

还要记住,您可以访问所有 J​​ava...

好吧,也许我应该提供一些示例...我的 clojure 技能不好,因此这些示例可能需要一些调整。

System.console() 方式:

(let [console (. System console)
     pwd (.readPassword console "tell me your password: ")]
   (println "your password is " pwd))

BufferedReader 方式:

(print "give me a line: ")
(let [reader (java.io.BufferedReader. *in*)
     ln (.readLine reader)]
   (println "your line is " ln))

我的观点是,人们可以在 Clojure 中利用 Java 知识以及 Java 本身。 这是其主要的、广为人知的优势之一。

想知道如果问题是关于 GUI 中的用户输入,我的分数会是多少!

顺便说一句,您可以使用 JOptionPane 建立一个小 GUI 来获取用户输入......

Remember also that you have access to all of Java ...

OK so perhaps I should present some examples ... my clojure skills are not good so these examples may need a bit of tweaking.

The System.console() way:

(let [console (. System console)
     pwd (.readPassword console "tell me your password: ")]
   (println "your password is " pwd))

The BufferedReader way:

(print "give me a line: ")
(let [reader (java.io.BufferedReader. *in*)
     ln (.readLine reader)]
   (println "your line is " ln))

My point is that one can leverage knowledge of Java, and Java itself, in Clojure. It's one of its principal, advertised strengths.

Wonder what would my score have been if the question were about user input from a GUI!

By the way, you could use JOptionPane to put up a little GUI to get user input ...

黎夕旧梦 2024-08-02 22:39:34

read-line 用于获取用户输入并使用 let 将其绑定到某个变量。

例如:如果你想从用户读取用户ID和密码并显示它,你可以使用下面的代码

(defn print-message [pid pw] 
(println "PID : " pid)
 (println "PW : " pw))

(defn inp[]
(println "Enter your PID and password") 
(let[pid (read-line) pw (read-line)] 
(print-message pid pw) ))

read-line is used to get user input and use let to bind it to some variable.

For example : if you want to read user ID and password from user and display it, you could use the following piece of code

(defn print-message [pid pw] 
(println "PID : " pid)
 (println "PW : " pw))

(defn inp[]
(println "Enter your PID and password") 
(let[pid (read-line) pw (read-line)] 
(print-message pid pw) ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文