总是返回不正确的简单数学lua

总是返回不正确的简单数学lua

本文介绍了总是返回不正确的简单数学lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的Lua脚本,会问一个数学问题

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?

推荐答案

answer包含字符串,因此从不等于数字.

answer contains a string and so is never equal to a number.

只需添加

    answer = tonumber(answer)

之后

    answer = io.read()

这篇关于总是返回不正确的简单数学lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:54