【发布时间】:2017-03-26 12:51:13
【问题描述】:
我真的需要帮助。我仍然无法弄清楚我的逻辑 lua 代码如何使用 if 条件阻止事件,当它已经有一个值时?
这是我的 array2d:
mydata = {
{"mike", "30", "1"},
{"michael", "40", "2"},
{"mike", "40", "2"},
{"michael", "50", "3"},
{"frost", "50", "3"},
{"nick", "60", "4"}
}
实际上,我的计划是获取正确的代码来检查索引名称mydata[..][1]和点索引mydata[..][3]。
这是一个示例案例说明:
所以如果索引名称Michael和点索引3不存在或 如果只有名称michael存在但是它没有点索引3,那么它会在data.txt中写入michael:50:3。 但如果名称和点索引已经存在。然后,它会阻塞并打印(“对不起,你的名字和分数索引已经存在”)
我的代码:
mydata = {{"mike", "30", "1"},
{"michael", "40", "2"},
{"mike", "40", "2"},
{"michael", "50", "3"},
{"frost", "50", "3"},
{"nick", "60", "4"}}
function check_point1(tab, val1) -- Function for checking name
for index, value in ipairs (tab) do
if value[1] == val1 then
return true
end
end
return false
end
function check_point2(tab, val1) -- Function for checking player point after name
for index, value in ipairs (tab) do
if value[3] == val1 then
return true
end
end
return false
end
-- this is example case
-- these below are variable to process checking if it's exist
name = "michael"
id = "3"
-- this is my checking code, idk it's good or not. or maybe it's wrong code. because it's not working
if check_point1(mydata, name) then
if check_point2(mydata, id) then
print(your name and point index are exist. sorry you can't earn this point")
else
print(only your name exist. let us save your new point now")
end
else
print("your name and point index is not exist. let us save your name and point now")
local file = io.open("data.txt", "r")
file:write("michael:50:3")
file:close()
end
当事件“写michael:50:3”已经存在时,阻止它的最佳方法是什么?在你帮助我之前,让我知道你是否需要了解一些事情。我在 lua 方面仍然很差。
【问题讨论】:
-
Han,你现在已经问了几个类似的问题,它们似乎都围绕着基本的程序流程、简单的算法和标准的 Lua 库。提问很好——它可以帮助你学习——但你也可能会发现通过采用更通用的学习方法可以学得很好。请务必阅读Programming in Lua,将Reference Manual 放在手边,并考虑参加当地的大学课程,或者在线学习编程课程,因为一些通用编程课程会有很长的路要走。
-
是的,我知道,我问了很多次。我应该尝试自己学习更多,但是 idk。我已经尝试了很多次并尝试思考 lua 是如何工作的,但是 lua 真的很难。所以我问更多问题不是因为我想完成我尝试的一些脚本。但这可以成为我的教训。实际上,当我第一次提出问题时,有人回答了它的工作原理并解释了我的代码无法正常工作的原因。它对我的学习帮助很大。我不仅在学习 lua,而且还在学习大多数编程语言,如 c#、java 等,我也想学习 lua,但我只需要帮助。因为它不同
-
比其他语言。
标签: arrays multidimensional-array lua lua-table