java调用julia脚本,Julia请求脚本中的用户输入

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

result = input(prompt)

谢谢

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

我们可以为此拥有一个更复杂的readline-library-like系统,但是现在这可以解决问题。 Keno的纯Julia重新实现我们的repl将为这样的交互式东西提供一个很好的框架。

在julia 0.7及之后(可能是0.6),现在stdin。

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

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>

很好的答案,这有很大帮助。

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

功能定义:

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:");

首先,我跑了

Pkg.add("日期")

然后

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)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值