[英]Lua string comparison not working
Well I am learning Lua at the moment and I wanted to write a little script. 好吧,我现在正在学习Lua,我想写一个小脚本。 It's just for practice and understanding how Lua is working.
这只是为了练习和理解Lua是如何工作的。
local name = io.read()
if name == Test
then print("Right")
else print("Wrong")
end
Normally the output should be "Right" if I enter "Test" but it always prints "Wrong". 通常,如果我输入“Test”,输出应该是“Right”,但它总是打印“Wrong”。 I tried it many times and wrote the code in other forms but didn't get my solution.
我尝试了很多次,并以其他形式编写代码,但没有得到我的解决方案。
Can anyone help me please? 有人可以帮我吗?
You're missing a set of quotation marks. 你错过了一组引号。
This: 这个:
if name == Test
compares the values of two variables, name
and Test
. 比较两个变量的
name
, name
和Test
。
You want this: 你要这个:
if name == "Test"
Lua doesn't require variables to be declared, so this is an easy mistake to make. Lua不需要声明变量,因此这是一个容易犯的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.