我在Julia创建了一个儿科医疗剂量计算器。该程序允许用户输入患者的体重、剂量频率等,以获得数字输出。
然而,一旦计算出数字输出,有没有办法让用户选择重新运行程序并输入新的信息?例如,如果医生想要对不同的患者进行新的计算,他们可以输入"yes",否则可以输入"exit“。
什么功能最适合完成此任务?
发布于 2021-06-17 15:44:01
您可以使用break
中断无限的while
循环来做到这一点。
while true
print("What's your name ? \n");
name = readline()
println("The name is ", name)
print("\n\n")
print("Continue ? [y/n]\n")
# maybe check if the answer is not "y" or "n"
answer = readline()
if answer == "n"
break
end
end
发布于 2021-06-17 15:58:52
我可能会这样做:
function pmd_calculator()
# ask data and calculate stuff
function final_question()
println("Run program again for new patient? [y|n]")
answer = readline()
if answer == "y"
pmd_calculator()
elseif answer == "n"
println("K, bye!")
else
println("invalid answer, try again.")
final_question()
end
end
final_question()
end
发布于 2021-06-19 16:52:25
这是一种经常在后台使用电子表格或数据库完成的事情。如果您在笔记本电脑上运行此应用程序,而不是作为web应用程序,则可以将其设置为Gtk应用程序:
using Gtk
function dosecalculator(func::Function, rlabel = "Results")
wentry, aentry, hentry, qentry = GtkEntry(), GtkEntry(), GtkEntry(), GtkEntry()
weightunits = [GtkRadioButton("kg")]
push!(weightunits, GtkRadioButton(weightunits[1], "lb"))
ageunits = [GtkRadioButton("years")]
push!(ageunits, GtkRadioButton(ageunits[1], "months"))
heightunits = [GtkRadioButton("cm")]
push!(heightunits, GtkRadioButton(heightunits[1], "in"))
resultbutton = GtkButton("Calculate")
resultlabel = GtkLabel("0")
win = GtkWindow("Dose Calculator", 500, 100) |> (GtkFrame() |> (vbox = GtkBox(:v)))
wbox = GtkButtonBox(:h)
push!(wbox, GtkLabel("Patient Weight"), wentry, weightunits...)
abox = GtkButtonBox(:h)
push!(abox, GtkLabel("Age"), aentry, ageunits...)
hbox = GtkButtonBox(:h)
push!(hbox, GtkLabel("Height"), hentry, heightunits...)
resultbox = GtkButtonBox(:h)
qbox = GtkButtonBox(:h)
push!(qbox, GtkLabel("Give medication every: "), qentry, GtkLabel(" hours."))
set_gtk_property!(qentry, :text, "12")
push!(resultbox, GtkLabel(rlabel), resultlabel, resultbutton)
push!(vbox, wbox, abox, hbox, qbox, resultbox)
function calculate(w)
wt = parse(Float64, get_gtk_property(wentry, :text, String)) /
(get_gtk_property(weightunits[1], :active, Bool) ? 1 : 0.393701)
ht = parse(Float64, get_gtk_property(hentry, :text, String)) *
(get_gtk_property(heightunits[1], :active, Bool) ? 1 : 2.20462)
ag = parse(Float64, get_gtk_property(aentry, :text, String)) /
(get_gtk_property(ageunits[1], :active, Bool) ? 1 : 12)
result = func(weight = wt, height = ht, age = ag)
dosagefraction = parse(Float64, get_gtk_property(qentry, :text, String)) / 24
GAccessor.text(resultlabel, " ")
GAccessor.text(resultlabel, string(result * dosagefraction))
end
signal_connect(calculate, resultbutton, :clicked)
cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
showall(win)
wait(cond)
end
f(; age = 30, weight = 70, height = 200, args...) = age * weight * height
dosecalculator(f)
在这里,您希望传递给Gtk应用程序的函数以命名形式(weight = 70.5等)接受参数,并为您想要进行的计算定制函数。(我给出的示例只是全部相乘。)
https://stackoverflow.com/questions/68011565
复制相似问题