2
您好,我正在嘗試在文件中讀入一個單詞列表,然後讓用戶在命令行上輸入一個數字來選擇他的單詞。但是,當我運行我的代碼時,它不允許我輸入數字。這裏是我的代碼:從文件中讀取命令行輸入
function readFile(file)
array = {}
io.input(file)
line = io.read()
table.insert(array, line)
while true do
line = io.read()
if line == nil then break end
table.insert(array, line)
end
length = #array
count = 1
while count <= length do
print(count .. ". " .. array[count])
count = count + 1
end
return array
end
function chooseWord(wordArr)
local answer
io.write("Select a Number to choose a word")
answer = io.read()
local word = wordArr[answer]
print(word)
return word
end
words = readFile("dictionary.txt")
word = chooseWord(words)
如果我刪除READFILE功能我可以輸入完全正常,但一旦我讀的文件,我
太棒了!謝謝! – user2525608