您问题的代码中要注意的第一项是:
(String)in = (String)Readline(STDIN)
Julia 1.0.0 现在使用stdin
代替STDIN
.
接下来,(String)
在 Julia 中,类型转换不是您需要或想要做的事情。
因此您的代码可以读取(尽管我们收到错误):
julia> in = readline(stdin)
This is a test.
ERROR: cannot assign variable Base.in from module Main
如此多变in
与 Julia 发生冲突Base.in
多变的。只需使用另一个变量名称即可。
julia> response = readline(stdin)
This is a test.
"This is a test"
该代码现在可以运行,但没有提示。您的答案提供了一个示例输入函数,其中包含您定义的提示,如下所示:
julia> function input(prompt::AbstractString="")
print(prompt)
return chomp(readline())
end
input (generic function with 2 methods)
The chomp
函数删除单个尾随\n
输入字符串中的换行符。文档here https://docs.julialang.org/en/v1/base/strings/#Base.chomp.
该函数的使用示例如下:
julia> input_file = input("Please enter input file name: ")
Please enter input file name: Data.txt
"Data.txt"
julia> output_file = input("Please enter output file name: ")
Please enter output file name: Output.txt
"Output.txt"
命令行参数方法
As the docs https://docs.julialang.org/en/latest/manual/getting-started/#man-getting-started-1指出,要打印出给脚本的参数,您可以执行以下操作:
println("Arguments passed to ", PROGRAM_FILE, ":")
for arg in ARGS
println(arg)
end
以下是在 Windows 命令行上运行上述代码的示例:
c:\OS_Prompt>julia PrintArgs.jl Data.txt Output1.txt Output2.txt Output3.txt
Arguments passed to PrintArgs.jl:
Data.txt
Output1.txt
Output2.txt
Output3.txt
您还可以打印出脚本文件名,如图所示,PrintArgs.jl。