gpt4 book ai didi

lua - Lua中的if语句

转载 作者:行者123 更新时间:2023-12-04 14:03:25 30 4
gpt4 key购买 nike

我正在尝试做最简单的事情:

  • 程序打印第一条消息并等待用户输入
  • 用户输入“play”或“leave”
  • 如果用户输入“play”程序打印“let's play”并退出(暂时)
  • 如果用户输入“离开”程序打印“再见”并退出
  • 如果用户键入不同于“播放”或“离开”程序的内容打印第一条消息并再次等待用户输入

但是当前代码只打印第一条消息 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

这里有什么问题吗?任何帮助表示赞赏,谢谢

最佳答案

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

阅读更多关于循环的信息 here .

关于lua - Lua中的if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634698/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com