使用用户输入的 Lua

我在 Lua 代码中哪里出错了? 我想生成两个随机数,并询问它们相乘的结果是多少。 第一部分没问题,但无论我输入什么答案,它都认为是错误的。 请告诉我我做错了什么以及如何修复它。请记住这是 Lua 代码。

math.randomseed(os.time())
local a=math.random(10)
local b=math.random(10)

local answer
repeat
io.write("What is ",a,"*",b,"?")
io.flush()
answer=io.read()
if answer==a*b then
print("Correct!")
else
print("Try Again")
end
until
answer==a*b
点赞
用户582
用户582

io.read() 返回一个字符串,你正在将其与一个数字进行比较。

你需要使用 answer=tonumber(io.read()) 或者使用 io.read("*n")

2013-03-15 21:56:19