我正在编写代码并运行它,但它不起作用。它没有向我显示错误消息,所以我不知道出了什么问题。任何人都可以解决这个问题吗?
这是代码。
math.randomseed(os.time())
value = {"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
1, 2, 3, 4, 5, 6, 7, 8, 9}
function makepassword(v)
for i = 1, v do
local v = math.random(1, 35)
local ud = math.random(0, 1)
if ud == 1 then
local p = string.upper(value[v])
else
local p = value[v]
end
if(not a) then
a = p
else
a = a..p
end
return a
end
start = makepassword(18)
print(start)
请帮我。
有未封闭的for
运算符,这就是它不起作用的原因。但仅仅关闭它是不够的。以下是进一步调整代码的一些方法:
math.randomseed(os.time())
-- a little more compact way of storing character sets is strings:
local charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz123456789"
-- store the length not to calculate it every time:
local charset_length = string.len(charset)
function makepassword(length) -- here was duplicated variable name v
local a = "" -- no reason to use global a
for i = 1, length do
local v = math.random(1, charset_length)
a = a .. charset:sub(v, v)
end
return a
end
start = makepassword(18)
print(start)
老实说,我不喜欢太多以这种方式编码的字符。有Lua的功能string.char(),它给你一个角色从它的代码,例如string.char(48) == "0"
,string.char(65) == "A"
等等。所以,如果你不回避特殊字符的故意,这一切都可以更简单:
function makepassword(length)
local a = ""
for i = 1, length do
a = a .. string.char(math.random(33, 126))
end
return a
end
print(makepassword(18))
欢迎 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句