【问题标题】:Computercraft variables计算机技术变量
【发布时间】:2014-01-18 03:10:55
【问题描述】:

我正在 Minecraft 上开一家银行。

在对变量进行加减运算后,我无法保存变量。

例如,如果 x="balance", x=15,说我想从我的余额中提现:

x = 15 - y(withdrawn money)

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

【问题讨论】:

  • 您的问题不清楚。 x是什么?一个保持平衡的变量? y 是什么?
  • x 是一个人的余额 y 是从账户中取出的钱
  • 修改您的问题以使其更清晰,而不是添加 cmets

标签: lua minecraft computercraft


【解决方案1】:

如果您希望程序运行之间的数据持久性,您需要将数据存储在文件中。例如,您可以将变量 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

【讨论】:

    【解决方案2】:

    这是第一次尝试。我想帐户余额存储在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      相关资源
      最近更新 更多