【问题标题】:lua code working, but stops before code is finishedlua 代码工作,但在代码完成之前停止
【发布时间】:2019-05-02 07:13:00
【问题描述】:

我正在为 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

我希望它会打印出“好的,请输入...的更正版本”,但它甚至没有用。

【问题讨论】:

  • 首先end太多了,最好用elseif代替answer2 == 'no'。在这种情况下,您永远不会加入该子句。其次,您正在校对answer3 的变量不存在。也许您忘记了字符串引号,例如answer3 == 'FN'。此外,您忘记了 answerFNanswerLN 的本地 var 定义......因此,如果您之前没有定义它们,请确保定义它们。至少我会建议您编写一个函数来打印此标准字符串输出 (You are bla bla you live in ...')。

标签: lua


【解决方案1】:

您可能希望将answer3 == VG 更改为answer3 == "VG"(以及其他)。目前,它正在与名称为VG 的变量进行比较,该变量可能不存在。

【讨论】:

    【解决方案2】:

    在使用'aww, I was wrong. Do you want to enter the information again? Say yes or no.' 消息提示用户输入“是”或“否”后,您询问需要进行哪些更改并将用户输入存储在answer3 变量中。

    但是您将answer3 的值与FNLN 等其他变量 进行比较,而不是"FN""LN" 等字符串。

    Lua 不会抱怨这一点,因为未定义的变量被认为具有 nil 值。


    此外,当仅更改了 FNLNHT 时,您使用了未定义的变量 answerVG。请改用VideoGame 变量。


    在比较answer3 的值时,您可以使用 if-else 阶梯,而不是使用不同的 if .. ends,例如

    if <condition1> then
        ...
    elseif <condition2> then
        ...
    else
        ...
    end
    

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2014-01-20
      • 2021-07-15
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多