我正在尝试使用 Visual Studio 代码在 Julia 中编写一个简单的 BMI 计算器。我希望程序首先询问用户是否喜欢“公制”或“英制”单位,然后根据用户的回答定制后续问题。关于构建这个的最佳方法有什么建议吗?例如,我尝试过:
println("Welcome, what is your name?")
user_name = readline(stdin)
println("Hello $user_name, nice to meet you.")
println("Please choose whether you would like to use metric or imperial units:"
preferred_units = readline(stdin)
function preference()
if startswith(preferred_units, "imperial")
println("Enter your weight in lbs:")
weight_imperial = parse(Float64, deadline(stdin))
else
if startswith(preferred_units, "metric")
println("Enter your weight in kg:")
weight_metric = parse(Float64, deadline(stdin))
end
是否有更合适的不同功能?
最佳答案
由于一些微小的拼写错误/错误,您的代码对我来说是错误的。这对我有用:
println("Welcome, what is your name?")
user_name = readline(stdin)
println("Hello $user_name, nice to meet you.")
println("Please choose whether you would like to use metric or imperial units:")
preferred_units = readline(stdin)
function preference()
if startswith(preferred_units, "imperial")
println("Enter your weight in lbs:")
weight_imperial = parse(Float64, readline(stdin))
elseif startswith(preferred_units, "metric")
println("Enter your weight in kg:")
weight_metric = parse(Float64, readline(stdin))
end
end
preference()
关于string - 在 Julia 中搜索字符串并返回条件输出的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67995091/