LUA是新手,我对我的剧本有疑问

LUA是新手,我对我的剧本有疑问,lua,Lua,您好,我是LUA的新手,通常是脚本/编码的新手 这就是我要说的 print("Type a Number!") repeat input = io.read() if input == "10" then print("Ten") elseif input == "7" then print("Seven") elseif input == "1"

您好,我是LUA的新手,通常是脚本/编码的新手 这就是我要说的

print("Type a Number!")
repeat
input = io.read()
if input == "10" then 
    print("Ten")
elseif input == "7" then
        print("Seven")

elseif input == "1" then
    print("One!")
elseif input == "exit" or input == "Exit" then
    print("Exiting...")  
else
     print("incorrect")  
end
until input == "Exit" or input == "exit"
我觉得elseif太多了,但我不想在键入exit或exit时打印“不正确”,所以我的解决方案是在else命令之前添加另一个elseif。这能更简单些吗?或者我不能用它做任何事,它很好

还有一个问题,为什么这不起作用

num = 10
input = io.read()
If input == num then 
     print("Ten")
end

还是这个代码

num = 10
input = io.read()
If input == 10 then 
     print("Ten")
end
为什么上面的代码在if后面有带字符串的布尔值,只起作用

本地输入={
['1']='1!'
,['7']='7'
,['10']='10'
,exit='正在退出…'
,Exit='正在退出…'
}
本地输入
打印“键入一个数字!”
重复
输入=io.read()
打印(输入[输入]或“不正确”)--Lua“nullsafe”运算符。
直到输入==“退出”或输入==“退出”
  • 定义你的行为
  • 本地操作={
    [1] =函数()打印(“一”);
    --其他号码也一样
    [“exit”]=function()os.exit()end;--关闭整个程序
    }
    
  • 获取您的输入
  • local input=io.read()--返回一个字符串
    
  • 规范化您的输入
  • input=input:lower()--使整个字符串小写
    input=tonumber(输入)或input——尝试转换为数字
    
  • 获取与输入匹配的操作
  • 本地操作=操作[输入]
    
  • 如果找到,请运行该操作
  • if动作然后
    行动()
    其他的
    打印(“错误!无法处理输入:”,输入)
    结束
    
    1)
    If
    If
    不同。2)
    10
    “10”
    3)不同
    io.read()
    返回字符串,您需要
    tonumber()
    将其转换为数字,然后再与另一个数字进行比较。实际上,其他两个代码示例不起作用是有道理的,我不知道io.read()仅用于字符串…是否有类似的数字类型,或者我总是必须将其转换为数字?谢谢你的回答,非常有帮助:))