我试着做最简单的事情:
但是,当前代码只打印第一条消息2次,并退出:
print("welcome. you have 2 options: play or leave. choose.")
input = io.read()
if input == "play" then
print("let's play")
end
if input == "leave" then
print("bye")
end
if input ~= "play" or "leave" then
print("welcome. you have 2 options: play or leave. choose.")
end这里怎么了?感谢您的帮助,谢谢
发布于 2017-10-08 19:27:28
if语句只执行一次。它不会跳到程序的其他部分。要做到这一点,您需要将输入代码包装在一个while循环中,并在得到有效的响应时退出:
while true do
print("welcome. you have 2 options: play or leave. choose.")
local input = io.read()
if input == "play" then
print("let's play")
break
elseif input == "leave" then
print("bye")
break
end
end阅读更多关于循环这里的信息。
发布于 2017-10-08 19:26:00
行if input ~= "play" or "leave" then被计算为:
if (input ~= "play") or "leave" then字符串"leave"或任何用于该事项的字符串都被认为是一个真实的值。
您需要使用and比较这两个字符串
if input ~= "play" and input ~= "leave" then
print("welcome. you have 2 options: play or leave. choose.")
end发布于 2018-08-11 17:17:52
您需要一个循环,就像您在这里说的那样,如果输入没有退出,那么重做代码,而其他则意味着它将检查if,否则,最后,检查else。
print("welcome. you have 2 options: play or leave. choose.")
while input ~= "exit" do
input = io.read()
if input == "play" then
print("let's play")
elseif input == "leave" then
print("bye")
else
print("welcome. you have 2 options: play or leave. choose.")
end
endhttps://stackoverflow.com/questions/46634698
复制相似问题