计算机技术变量

时间:2013-09-01 01:46:36

标签: lua minecraft computercraft

我在Minecraft上开了一家银行。

我在添加或减少变量后保存变量时遇到问题。

例如,如果x =“balance”,x = 15,说我想退出余额:

x = 15 - y(withdrawn money)

再次运行程序时,不保存变量。

2 个答案:

答案 0 :(得分:3)

如果要在程序运行之间保持数据持久性,则需要将数据存储在文件中。例如,您可以将变量x保存到这样的文件中:

h = fs.open("filename","w")
h.writeLine(x)
h.close()

你可以像这样加载它:

h = fs.open("filename","r")
x = tonumber(h.readLine())
h.close()

这是文档。 http://computercraft.info/wiki/Fs.open

答案 1 :(得分:0)

这是对它的第一次尝试。我想帐户余额存储在x中。然后,以下函数将撤回并从x返回资金。

-- wa is amount to withdraw
-- this function withdraws the maximum allowable
function withdraw(wa) 
    if wa>0 then
        wt=math.min(x,wa)
        if wa <= x then
            x=x-wt
            return wt
        end
    end
    return 0
end

PiL书中提供了一种更为复杂的帐户保存方式:http://www.lua.org/pil/16.html