2016-02-12 76 views
3

我想问几次给用户输入整数,浮点数和有时分数。 我一直在阅读文档,但我很困惑什么是存储用户输入的最佳方式。我应该使用read,readline还是其他的东西? 让我们考虑整数:Julia:读,readline还是别的?

print("Input an integer: ") 
n = read(STDIN,UInt8) 
println(n)      #returns the ASCII number correspondent 
            #to the first input character 

print("Input an integer: ") 
n = parse(UInt8,readline(STDIN)) 
println(n)      #returns the input number correctly 
            #but I wonder if there is a better way to do it 
+1

'readline'看起来很稳固。 –

+1

[Julia从脚本请求用户输入]的可能重复(http://stackoverflow.com/questions/17479782/julia-request-user-input-from-script) – SalchiPapa

+1

@ user3580870不,它不是:(,它doesn例如没有GNU readline支持,这意味着你不能像使用“右箭头”(例如纠正错字)那样沿着输入行移动光标,你会得到'^ [[D' ,另见:[#9867](https://github.com/JuliaLang/julia/pull/9867)和[#9851](https://github.com/JuliaLang/julia/pull/9851)我会试着再次实现这一点,现在我明白了新的REPL是如何工作的......感谢[LispREPL](https://github.com/swadey/LispREPL.jl/blob/master/src/LispREPL.jl)! :D – SalchiPapa

回答

0

通常的方式做到这一点是使用readline功能。请注意,STDIN参数不是必需的,因为readline将默认从STDIN读取。

print("Input an integer: ") 
n = parse(UInt8, readline()) 

上的方式来获取输入从标准输入流的详细信息,请参阅Input