[英]lua code working, but stops before code is finished
我正在為 lua 編寫用戶輸入代碼,如果您在輸入信息時出錯,您可以通過說出您要修復的內容來修復它。 在你輸入這兩個字母后(例如說你拼錯了 england 並拼寫為 englaund,你可以輸入 HT 來修復它。)它會提示你修復它,並且在你這樣做之后它只是說代碼已完成,即使它不是。
我試過使變量成為本地變量,使塊都是 ifs 而不是 elseifs。
--user input--
print('Hello, what is your name? ')
local name = io.read()
print('What is your last name?')
local LastName = io.read()
print('The place you live?')
local Hometown = io.read()
print('Lastly, what is your favourite video game?')
local VideoGame = io.read()
--Printing the information--
print(
'You are ' .. name .. ' ' .. LastName ..
' you live in ' .. Hometown ..
' and your favourite video game is ' .. VideoGame .. '.'
)
print('right?')
-- confirmation --
io.write("press 1 i was correct, and press 2 if i was wrong.")
answer = io.read()
if answer == "1" then
print('Yay, I was correct!')
elseif answer == "2" then
print('aww, I was wrong. Do you want to enter the information again? Say yes or no.')
local answer2 = io.read()
if answer2 == "yes" then
print('What would you like to change? Type either FN, LN, HT or VG to change which one you would like.')
local answer3 = io.read()
if answer3 == FN then
io.write('Ok, please enter the corrected version of your first name.')
answerFN = io.read()
io.write('Here is the corrected version.')
io.write(
'You are ' .. answerFN .. ' ' .. LastName ..
' you live in ' .. Hometown ..
' and your favourite video game is ' .. answerVG .. '.'
)
end
if answer3 == LN then
print('Ok, please enter the corrected version of your last name.')
answerLN = io.read()
print('Here is the corrected version.')
print(
'You are ' .. name .. ' ' .. answerLN ..
' you live in ' .. Hometown ..
' and your favourite video game is ' .. answerVG .. '.'
)
end
if answer3 == HT then
print('Ok, please enter the corrected version of your hometown.')
answerHT = io.read()
print('Here is the corrected version.')
print(
'You are ' .. name .. ' ' .. LastName ..
' you live in ' .. answerHT ..
' and your favourite video game is ' .. answerVG .. '.'
)
end
if answer3 == VG then
print('Ok, please enter the corrected version of your favourite video game.')
answerVG = io.read()
print('Here is the corrected version.')
print(
'You are ' .. name .. ' ' .. LastName ..
' you live in ' .. Hometown ..
' and your favourite video game is ' .. answerVG .. '.'
)
end
if answer2 == "no" then
print('Alright, tough luck. You can run the code again if you change your mind.')
end
end
end
我希望它打印出“好的,輸入...的更正版本”,但它甚至沒有用。
您可能需要將answer3 == VG
更改為answer3 == "VG"
(以及其他)。 當前,它正在與名稱為VG
的變量進行比較,該變量可能不存在。
在提示用戶使用“ aww”輸入“是”或“否”后'aww, I was wrong. Do you want to enter the information again? Say yes or no.'
'aww, I was wrong. Do you want to enter the information again? Say yes or no.'
消息時,您詢問需要進行哪些更改並將用戶輸入存儲在answer3
變量中。
但是,您正在將answer3
的值與其他變量(例如FN
, LN
等)而不是類似"FN"
和"LN"
的字符串進行比較。
Lua對此並不抱怨,因為未定義的變量被認為具有nil
值。
另外,僅更改FN
或LN
或HT
時,您使用了未定義的變量answerVG
。 請改用VideoGame
變量。
當比較值answer3
而不是使用不同的, if .. end
S,你可以使用if-else的階梯狀:
if <condition1> then
...
elseif <condition2> then
...
else
...
end
您似乎使用變量而不是字符串來重新輸入信息,但變量未設置為任何值。 您還針對重新輸入信息的每個條件使用單獨的 if 語句,這會減慢程序的加載速度。
相反,你可能想把
if answer3 == "FN" then
...
elseif answer3 == "LN" then
...
elseif answer3 == "HT" then
...
elseif answer3 == "VG" then
...
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.