【问题标题】:lua calling the same function as a looplua 调用与循环相同的函数
【发布时间】:2020-04-30 11:33:34
【问题描述】:

我正在制作一个 D&D 项目,这是统计部分。我想调用 stats1 函数进行重新滚动。谁能帮我?

function stats1()


Strength = math.random(1,20) 
  Dexterity = math.random(1,20) 
  Constitution = math.random(1,20)
  Intelligence = math.random(1,20) 
  Wisdom = math.random(1,20) 
  Charisma = math.random(1,20) 
  print("           Stats            ")
  print("--------------------------")
  print("|  Strength  |     "..Strength.."      |")
  print("--------------------------")
  print("| Dexterity  |     "..Dexterity.."      |")
  print("--------------------------")
  print("|Constitution|     "..Constitution.."      |")
  print("--------------------------")
  print("|Intelligence|     "..Intelligence.."      |")
  print("--------------------------")
  print("|   Wisdom   |     "..Wisdom.."      |")
  print("--------------------------")
  print("|  Charisma  |     "..Charisma.."      |")
  print("--------------------------")
  print("Reroll stats?")
  reroll = io.read
  if reroll == "y" or "Y" then
    for Re_Roll = true
      stats()
      if reroll == "n" or "N" then
  Re_Roll = false
  end
end
  else 
      print("thanks for being cultured")
    end

如果可以的话,我会很高兴收到您的反馈。谢谢!

【问题讨论】:

  • reroll = io.read()

标签: function loops lua


【解决方案1】:

你的函数有很多错误

  1. reroll = io.read。这会将函数io.read 分配给reroll,而不是读取值的值。您需要调用io.read 并将reroll 设置为结果值。

    reroll = io.read()
    

    如 @EgorSkriptunoff 在 cmets 中所述。

  2. 这种说法永远是正确的:

    if reroll == "y" or "Y" then
    

    您需要将两个字符串与reroll 进行比较,否则您将评估"Y" 的真实值,该值始终为真。

  3. for Re_Roll = true 将生成错误,因为它不是完整的 for 循环语句。看来你不打算在这里使用for,所以我建议删除它。

  4. 除了第 1 项的问题之外,您的 "N" 语句嵌套在您的第一个 if 语句中,这意味着它永远不会为真。

  5. else 语句位于 if 语句的末尾,这在 Lua 中是错误的,会导致异常。


您的代码不会尝试进行循环以允许重新滚动发生。我还建议阅读 lua 文档,我特别建议阅读 Programming in Lua 以帮助更好地理解该语言。

这是带有注释的更正的代码,以及一个允许重新滚动的 while 循环。

function stats()
  Strength = math.random(1, 20) 
  Dexterity = math.random(1, 20) 
  Constitution = math.random(1, 20)
  Intelligence = math.random(1, 20) 
  Wisdom = math.random(1, 20) 
  Charisma = math.random(1, 20)

  print("           Stats            ")
  print("--------------------------")
  print("|  Strength  |     " .. Strength .. "      |")
  print("--------------------------")
  print("| Dexterity  |     " .. Dexterity .. "      |")
  print("--------------------------")
  print("|Constitution|     " .. Constitution .. "      |")
  print("--------------------------")
  print("|Intelligence|     " .. Intelligence .. "      |")
  print("--------------------------")
  print("|   Wisdom   |     "..Wisdom .. "      |")
  print("--------------------------")
  print("|  Charisma  |     " .. Charisma .. "      |")
  print("--------------------------")
end

-- This will run the stat function then ask the user if they want to reroll.
stats()
while(true) do
  print("Reroll stats" .. (Re_Roll and " again" or "") .. "?")
  reroll = io.read()
  if reroll == "y" or reroll == "Y" then
    Re_Roll = true
    stats()
  elseif reroll == "n" or reroll == "N" then
    if not Re_Roll then
      print("thanks for being cultured")
    end
    Re_Roll = false
    break
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多