问题
I have a very basic Lua script that asks a math question
math.randomseed(os.time())
print ("Let's play Math")
a = math.random(1,10)
b = math.random(1,10)
io.write("What is " .. a .. " + " .. b .. "?")
answer = io.read()
correct = (a + b)
if (answer == correct) then
print ("Correct")
else
print ("Wrong")
print (correct) --For debug
end
For some reason, I am always getting "incorrect" even when answered correctly. I also print out the correct answer, just to make sure the program is handling the math correctly. Where is my mistake?
回答1:
answer
contains a string and so is never equal to a number.
Just add
answer = tonumber(answer)
after
answer = io.read()
来源:https://stackoverflow.com/questions/43532017/always-returning-incorrect-simple-math-lua