如何通过用户输入相加两个数字?
(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)))
答案 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
的结果解析为字符串,并将其分别分配给num1
和num2
,然后添加数字并打印结果。
请注意,在尝试传递给println
之前,无需将结果转换为字符串。如果您想这样做,str
应该放在括号内:(str (+ num1 num2))
。