通过接受用户输入将两个数字相加

时间:2019-09-24 08:50:01

标签: clojure

如何通过用户输入相加两个数字?

(println "What's first?")
(let [num1 ( read-line)]
(println (str num1)))
(println (str num1))
(println "What's sec?")
(let [num2 (read-line)]
(println (str num2))) 
(println str(+ num1 num2)))

1 个答案:

答案 0 :(得分:0)

(let [num1 (do (println "What's first?") (Integer/parseInt (read-line)))
      num2 (do (println "What's sec?") (Integer/parseInt (read-line)))]
  (println (+ num1 num2)))

do阻止返回最后一个表达式的求值结果。然后将read-line的结果解析为字符串,并将其分别分配给num1num2,然后添加数字并打印结果。

请注意,在尝试传递给println之前,无需将结果转换为字符串。如果您想这样做,str应该放在括号内:(str (+ num1 num2))