【问题标题】:tonumber returs nil and i can't find a solutiontonumber 返回 nil,我找不到解决方案
【发布时间】:2021-03-05 07:47:41
【问题描述】:

所以我在 Roblox 中做了一个高级计算器,但我卡在了等号按钮上+1) 或者它返回 nil、1+1 或者只是一个错误,但它永远不会返回我想要的 2。 代码如下:

-- Geral Variables
local Screen = game.workspace.Cauculator.Screen
local Potency2 = game.workspace.Cauculator.Potency2
local Potency3 = game.workspace.Cauculator.Potency3
local Potency4 = game.workspace.Cauculator.Potency4
local Minus = game.workspace.Cauculator.Minus
local Plus = game.workspace.Cauculator.Plus
local Percent = game.Workspace.Cauculator.Percent
local Times = game.Workspace.Cauculator.Mutiply
local SquareRoot = game.Workspace.Cauculator.SquareRoot
local Divide = game.Workspace.Cauculator.Divide
local Equal = game.Workspace.Cauculator.Equal
local Number = {}
local Result = nil
-- functions
function Press1 ()
    table.insert(Number, "1")
    game:GetService("LogService").MessageOut:Connect(function (M, T)
        
        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')
        
    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["1"].ClickDetector.MouseClick:Connect(Press1)

function Press2 ()
    table.insert(Number, "2")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["2"].ClickDetector.MouseClick:Connect(Press2)

function Press3 ()
    table.insert(Number, "3")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["3"].ClickDetector.MouseClick:Connect(Press3)

function Press4 ()
    table.insert(Number, "4")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["4"].ClickDetector.MouseClick:Connect(Press4)

function Press5 ()
    table.insert(Number, "5")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["5"].ClickDetector.MouseClick:Connect(Press5)

function Press6 ()
    table.insert(Number, "6")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["6"].ClickDetector.MouseClick:Connect(Press6)

function Press7 ()
    table.insert(Number, "7")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["7"].ClickDetector.MouseClick:Connect(Press7)

function Press8 ()
    table.insert(Number, "8")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["8"].ClickDetector.MouseClick:Connect(Press8)

function Press9 ()
    table.insert(Number, "9")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["9"].ClickDetector.MouseClick:Connect(Press9)

function Press0 ()
    table.insert(Number, "0")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["0"].ClickDetector.MouseClick:Connect(Press0)

function PressPlus ()
    table.insert(Number, "+")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
Plus.ClickDetector.MouseClick:Connect(PressPlus)

function PressMinus ()
    table.insert(Number, "-")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
Minus.ClickDetector.MouseClick:Connect(PressMinus)

function PressEqual ()
    
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Result = tonumber(string.gsub(tostring(M), '%s+', ''))
        print(Result)

    end)
    print(unpack(Number))
    
end
Equal.ClickDetector.MouseClick:Connect(PressEqual)```

【问题讨论】:

    标签: lua scripting lua-table roblox


    【解决方案1】:

    tonumber 不计算表达式,它的目的是从字符串中解析一个数字。

    要制作一个可以工作的计算器,您需要制作一个解析器来检测数字和数学运算符,然后执行评估逻辑。

    【讨论】:

    • @CarlosEduardoVeras 如果我回答了你,请将问题标记为“已回答”,这就是你在 StackOverflow 上的做法。 Read more
    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多