我的代码旨在读取数字并将其转换为中文拼音:
function digitconverter (digit)
if digit == "0" then
cnumber = "ying2 "
elseif digit == "1" then
cnumber = "yi1 "
elseif digit == "2" then
cnumber = "er2 "
elseif digit == "3" then
cnumber = "san1 "
elseif digit == "4" then
cnumber = "si4 "
elseif digit == "5" then
cnumber = "wu3 "
elseif digit == "6" then
cnumber = "liu4 "
elseif digit == "7" then
cnumber = "qi1 "
elseif digit == "8" then
cnumber = "ba1 "
elseif digit == "9" then
cnumber = "jiu3 "
end
return cnumber
end
print("Enter a number to be converted:")
repeat
strnumber = io.read("*line")
number = tonumber(strnumber)
if number ~= nil then
continue = true
else
print("Invalid input. Please try again:")
continue = false
end
until continue == true
nlength = #strnumber
digits = {}
for d in string.gmatch(number, "%d") do
digits[#digits + 1] = d
end
convnumber = ""
for d=1,nlength do
convnumber = convnumber .. digitconverter(digits[d])
end
print(convnumber)
io.read()
如果我输入超过 15 位数字,它就会卡住(因为没有更好的术语)。它会转换每个数字,但第 16 位将是随机的,第 17 位及以后将重复另一个随机数字。我已经完成了它,但我无法弄清楚它挂在哪里。想法?