Julia请求脚本中的用户输入

Julia request user input from script

如何从Julia中运行的脚本请求用户输入? 在MATLAB中,我会这样做:

1
result = input(prompt)

谢谢


最简单的事情是readline(stdin)。 这就是你要找的东西吗?


正如@StefanKarpinski所指出的那样,它将在未来得到解决,这就是我现在所做的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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>


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

功能定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

函数调用(用法):

1
2
sentence = getUserInput(String,"Write a sentence:");
n        = getUserInput(Int64,"Write a number:");

首先,我跑了
Pkg.add("日期")
然后

1
2
3
4
5
6
7
8
9
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)