Julia request user input from script

2019-01-25 03:08发布

问题:

How do I request user input from a running script in Julia? In MATLAB, I would do:

result = input(prompt)

Thanks

回答1:

The easiest thing to do is readline(stdin). Is that what you're looking for?



回答2:

As @StefanKarpinski points out, it's going to be adressed in the future, this is what I do for the time being:

julia> @doc """
           input(prompt::String="")::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::String="")::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::String="")::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>


回答3:

First I ran Pkg.add("Dates") then

using Dates

println()
print("enter year  "); year = int(readline(STDIN))
print("enter month "); month = int(readline(STDIN))
print("enter day   "); day = int(readline(STDIN))

date = Date(year, month, day)
println(date)


标签: julia-lang