string - 在 Julia 中搜索字符串并返回条件输出的最佳方法?

标签 string julia

我正在尝试使用 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/

相关文章:

string - 在 CL 中测试列表元素是否为字符串

Julia 默认数字类型

binary - Julia:基本功能比类似功能快 10,000 倍

julia - 在 Julia DataFrame 中查找一行

macros - 将表达式插入到引号内的表达式中

Java:用方法结果替换 RegEx

c# - C# 客户端和 C++ 服务器中套接字上的 Unicode 数据

java - java中可变字符串和不可变字符串有什么区别

julia - 如何防止 REPL 截断输出?

c++ - 什么是 c++11/14/17 等同于 ltoa/itoa 的 C 函数?