Julia从脚本请求用户输入

Julia从脚本请求用户输入,julia,Julia,如何从Julia中运行的脚本请求用户输入?在MATLAB中,我将执行以下操作: result = input(prompt) 谢谢最简单的方法是readline(stdin)。这就是你想要的吗?我喜欢这样定义它: julia> @doc """ input(prompt::AbstractString="")::String Read a string from STDIN. The trailing

如何从Julia中运行的脚本请求用户输入?在MATLAB中,我将执行以下操作:

result = input(prompt)

谢谢

最简单的方法是
readline(stdin)
。这就是你想要的吗?

我喜欢这样定义它:

julia> @doc """
           input(prompt::AbstractString="")::String

       Read a string from STDIN. The trailing newline is stripped.

       The prompt string, if given, is printed to standard output without a
       trailing newline before reading input.
       """ ->
       function input(prompt::AbstractString="")::String
           print(prompt)
           return chomp(readline())
       end
input (generic function with 2 methods)

julia> x = parse(Int, input());
42

julia> typeof(ans)
Int64

julia> name = input("What is your name? ");
What is your name? Ismael

julia> typeof(name)
String

help?> input
search: input

  input(prompt::AbstractString="")::String

  Read a string from STDIN. The trailing newline is stripped.

  The prompt string, if given, is printed to standard output without a trailing newline before reading input.

julia>
首先我跑 包装添加(“日期”) 然后


检查提供的答案是否与预期类型匹配的函数:

功能定义:

function getUserInput(T=String,msg="")
  print("$msg ")
  if T == String
      return readline()
  else
    try
      return parse(T,readline())
    catch
     println("Sorry, I could not interpret your answer. Please try again")
     getUserInput(T,msg)
    end
  end
end
函数调用(用法):


我们可以用一个更复杂的像readline库一样的系统来实现这一点,但现在这就足够了。基诺对我们的repl的纯Julia重新实现将提供一个很好的框架,用于执行类似这样的交互内容。在Julia 0.7和更高版本(可能是0.6)上,现在是
stdin
。回答不错,这非常有帮助。
function getUserInput(T=String,msg="")
  print("$msg ")
  if T == String
      return readline()
  else
    try
      return parse(T,readline())
    catch
     println("Sorry, I could not interpret your answer. Please try again")
     getUserInput(T,msg)
    end
  end
end
sentence = getUserInput(String,"Write a sentence:");
n        = getUserInput(Int64,"Write a number:");