预期'<name>'接近lua for loop </name>的最小值

时间:2014-07-17 04:25:51

标签: for-loop lua

function calculateGainOverHrs(dronenum, hrs)
    local yypx = 0
    for hrs=hrs,0,-1
    do
        yypx = yypx + calculateGainHour(dronenum)
    end
    return yypx
end

我试图创建一个程序,计算我从采矿设施获得多少钱(当然,(在游戏中)。)。我写了一个函数 - 上面的函数来计算一定时间内的增益。但是,在运行它时,我收到此错误 -

\LUAPrograms\miningalgoluaport.lua:29: '<name>' expected near '0'

这个问题的具体性意味着我无法搜索它。我确定有一个修复,但我只想让hrs参数输入到函数中。我做错了什么?

编辑:脚本如下 -

    -- Ores --
Cop = 5
Iro = 10
Sil = 150
Gol = 250
Pla = 400
Dia = 1000
Aeg = 1300
Cer = 1700
Rub = 2300
Ura = 1300
Plu = 2100
Sol = 4000
-- Ores --

drones = nil
totalpx = 0

math.randomseed(os.time * 42420451 * 425714 * 0.23)
math.randomseed(math.random(1,29214757862396238564328976))

function calculateGainOverDays(dronenum, days)
    yyypxyyy = calculateGainOverHrs(dronenum, 24) * days
    return yyypxyyy
end

function calculateGainOverHrs(dronenum, hrs)
    local yypx = 0
    for hrs,0,-1
  do
        yypx = yypx + calculateGainHour(dronenum)
    end
    return yypx
end

function calculateGainHour(dronenum)
    local ypx = 0
    orenum = math.random(1,30)
    for orenum,0,-1
    do
        ore = randint(1, 100)
        if (ore >= 1 and ore <= 5)
            px += Cop * dronenum
        end
        else if (ore >= 6 and ore <= 30)
            px += Iro * dronenum
        end
        else if (ore >= 31 and ore <= 52)
            px += Sil * dronenum
        end
        else if (ore >= 53 and ore <= 62)
            px += Gol * dronenum
        end
        else if (ore >= 63 and ore <= 70)
            px += Pla * dronenum
        end
        else if (ore >= 71 and ore <= 77)
            px += Dia * dronenum
        end
        else if (ore >= 78 and ore <= 87)
            px += Aeg * dronenum
        end
        else if (ore >= 88 and ore <= 90)
            px += Cer * dronenum
        end
        else if (ore == 91)
            px += Rub * dronenum
        end
        else if (ore >= 92 and ore <= 97)
            px += Plu * dronenum
        end
        else if (ore >= 98 and ore <= 100)
            px += Sol * dronenum
        end
    end
    return px
end

function menuDisplay()
    print
    (
    "1 - Calculate Ore Gain\n"..
    "2 - Change Drone Number\n"..
    "Total Pixels this Session: " .. tostring(totalpx)
    end
    )
    if (io.read = 1)
        print
        (
        "1 - Over Days \n"..
        "2 - Over Hours \n"..
        "3 - Over an Hour"
        )
        if (io.read() = 1)
            io.write("How many days?")
            local tempI = nil
            local tempII = io.read
            tempI = calculateGainOverDays(drones,tempII)
            print("The facility generated " .. tempI .. "px over" .. tempII .. "days.")
            totalpx = totalpx + tempI
            menuDisplay()
        end
        else if (io.read() = 2)
            io.write("How many hours?")
            local tempI = nil
            local tempII = io.read
            tempI = calculateGainOverHrs(drones,tempII)
            print("The facility generated " .. tempI .. "px over" .. tempII .. "hours.")
            totalpx = totalpx + tempI
            menuDisplay()
        end
        else if (io.read() = 3)
            local tempI = calculateGainHour(drones)
            print("In one hour, the facility generated " .. temp1 .. "px."
            totalpx = totalpx + tempI
            menuDisplay()
        end
    else if (io.read = 2)
        io.write("New Dronenum:")
        drones = io.read
        menuDisplay()
    end
end





--!CORE!--
--!CORE!--
--!CORE!--
--!CORE!--
--!CORE!--
print("Vernet Industries Ore Calculator - Created by Maurice Vernet")

print("Initializing...")

io.write("Enter Number of Drones:")

drones = io.read()

menuDisplay()

2 个答案:

答案 0 :(得分:3)

完整脚本中存在许多语法错误。以下是其中一些:

  1. 您在then语句中错过了if。另请注意,elseif是Lua中的关键字,它优先于else if,因为您不需要额外的end。例如:

    if (ore >= 1 and ore <= 5)
        --do something
    end       
    else if (ore >= 6 and ore <= 30)
        --do something else
    end
    

    应该是

    if ore >= 1 and ore <= 5 then
        --do something
    elseif ore >= 6 and ore <= 30 then
        --do something else
    end
    
  2. Lua中没有复合赋值,

    px += Iro * dronenum
    

    应该是

    px = px + Iro * dronenum
    
  3. io.read是函数本身,而io.read()是函数的返回值,您在多个地方使用它不正确。

答案 1 :(得分:0)

当我尝试将ia中的Lua代码复制并粘贴到我的文本编辑器(Sublime Text 3)时,我遇到了这个问题。事实证明,添加了特殊字符,可能会或可能不会显示。

我注意到编辑器中的语法看起来不正确所以我重新输入了一个函数。它不仅有效,而且语法与粘贴的代码不同。