Lua功能:为何简单功能引发误解?
- 内容介绍
- 文章标签
- 相关推荐
本文共计277个文字,预计阅读时间需要2分钟。
我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算:`property={a=120, b=50, c=85}`,`operator={has={a, b}, coefficient={a=0.45}}`。函数 `Result(x)` 返回 `operator.has.x * operator.coefficient.x`。
我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算:property = {a=120, b=50, c=85} operator = {has = {a, b}, coefficient = {a = 0.45}} function Result(x) return operator.has.x * operator.coefficient.x end print (Result(a)) error: attempt to perform arithmetic on field 'x' (a nil value)
问题是该函数在字面上尝试数学
“operator.has.x”而不是“operator.has.a”.
我能够调用函数(x)返回x.something结束,但如果我尝试函数(x)something.x我得到一个错误.我需要提高我对Lua中函数的理解,但我在手册中找不到这个.
我不确定你要做什么,但这里有一些基于你的代码的工作代码:property = {a=120, b=50, c=85} operator = {has = {a=2, b=3}, coefficient = {a = 0.45}} function Result(x) return operator.has[x] * operator.coefficient[x] end print (Result('a'))
打印’0.9′
本文共计277个文字,预计阅读时间需要2分钟。
我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算:`property={a=120, b=50, c=85}`,`operator={has={a, b}, coefficient={a=0.45}}`。函数 `Result(x)` 返回 `operator.has.x * operator.coefficient.x`。
我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算:property = {a=120, b=50, c=85} operator = {has = {a, b}, coefficient = {a = 0.45}} function Result(x) return operator.has.x * operator.coefficient.x end print (Result(a)) error: attempt to perform arithmetic on field 'x' (a nil value)
问题是该函数在字面上尝试数学
“operator.has.x”而不是“operator.has.a”.
我能够调用函数(x)返回x.something结束,但如果我尝试函数(x)something.x我得到一个错误.我需要提高我对Lua中函数的理解,但我在手册中找不到这个.
我不确定你要做什么,但这里有一些基于你的代码的工作代码:property = {a=120, b=50, c=85} operator = {has = {a=2, b=3}, coefficient = {a = 0.45}} function Result(x) return operator.has[x] * operator.coefficient[x] end print (Result('a'))
打印’0.9′

