朱莉娅:阅读、阅读行还是其他什么?

Pig*_*gna 5 input julia

我想多次要求用户输入整数、浮点数,有时还输入分数。我一直在阅读文档,但我对存储用户输入的最佳方式感到困惑。我应该使用 read、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
Run Code Online (Sandbox Code Playgroud)

Fen*_*ang 5

通常的方法是使用该readline函数。请注意,该STDIN参数不是必需的,因为readline默认情况下将从 STDIN 读取。

print("Input an integer: ")
n = parse(UInt8, readline())
Run Code Online (Sandbox Code Playgroud)

Input有关从标准输入流获取输入的方法的更多详细信息,请参阅参考资料。