tonumber 返回 nil,我找不到解决方案

Posted

技术标签:

【中文标题】tonumber 返回 nil,我找不到解决方案【英文标题】:tonumber returs nil and i can't find a solution 【发布时间】: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)```

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

@CarlosEduardoVeras 如果我回答了你,请将问题标记为“已回答”,这就是你在 *** 上的做法。 Read more

以上是关于tonumber 返回 nil,我找不到解决方案的主要内容,如果未能解决你的问题,请参考以下文章

AVCaptureDeviceInput在解包Optional值时意外发现nil

软件开发中的原表是啥呢?

条件不工作(不和谐)

可选链接

如何在 C# 中使用 GZipStream 解压缩?

最长递增子序列的潜在 O(n) 解