简体   繁体   English

Lua第二个嵌套if语句被忽略

[英]Lua 2nd nested if statement is ignored

I am writing a tic tac toe game for my assignment, and I was stuck with the nested if statement. 我正在为我的任务编写一个tic tac toe游戏,而我仍然坚持使用嵌套的if语句。 Attached part of my code below: 附上我的代码部分如下:

if name[1] == "AI" then 
     print("test string")
     playerId = 1
     if level == 1 or level == 2 then
        print("AI lvl 1/2")
        easy()
     elseif level == 3 then
        print("AI lvl 3")
        hard()
     end
elseif name[1] == "player" then
    print("test string2")
    Runtime:addEventListener("touch", fill)
end

while this executed, test string was printed on console, and playerId = 1, but inside 2nd if statement was completely ignored. 执行此操作时,测试字符串打印在控制台上,并且playerId = 1,但在第二个if语句中被完全忽略。 Not even print on console. 甚至不在控制台上打印。 Could someone help me solve it please? 有人可以帮我解决吗? Or tell me what is wrong with my code. 或者告诉我我的代码有什么问题。 Thanks 谢谢

Add something like print(level, type(level)) to see what level is. 添加类似print(level, type(level))以查看level Remember, lua can convert strings like '123' to numbers and vice versa when you want to do some arithmetics or string concatenations, but you can't compare strings with numbers like that: 123 == '123' will return false . 记住,当你想要做一些算术或字符串连接时,lua可以将像'123'这样'123'字符串转换为数字,反之亦然,但是你无法将字符串与这样的数字进行比较: 123 == '123'将返回false

So, if level is a string, either replace 1 , 2 and 3 in your code with '1' , '2' and '3' , or convert level to a number: level = tonumber(level) 因此,如果level是一个字符串,或者替换123在代码与'1''2''3' ,或转换level到一个数字: level = tonumber(level)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM