【问题标题】:Lua - How to print 2 things in one print statementLua - 如何在一个打印语句中打印两件事
【发布时间】:2019-10-17 06:45:50
【问题描述】:

在 python 中,您可以通过键入

使用一个语句来打印两件事

print("Hello" + " World")

输出将是"Hello world"

那么在 Lua 中是否有一个简单的方法可以再次执行此操作?

我试图让语句打印百分比和百分号。这是我目前拥有的

function update()
    local hp = crysHu.Health/ crysHu.MaxHealth
    local text = script.Parent.TextLabel
    healthBar:TweenSize(UDim2.new(hp,0,1,0),"In","Linear",1)
    text.Text = math.floor(hp*100)
end

text.Text = math.floor(hp*100) 是我在 FYI 方面需要帮助的部分。

text.Text = (math.floor(hp*100) + "%") 不起作用。

【问题讨论】:

  • 当您在 Lua 中寻找答案时,我删除了 Python 标签。
  • 请参阅 Lua 参考手册,这样您就不必在这里询问 Lua 的基本知识。这就是编写手册的目的。

标签: lua roblox


【解决方案1】:

如果您正在执行简单的字符串操作,您可以像这样将它们与.. 连接起来:

local foo = 100
print( tostring(foo) .. "%") -- 100%

或者如果你想要更具体的格式,你可以使用string.format

local foo = 100
print( string.format("%d%%", foo)) -- 100%

【讨论】:

    【解决方案2】:

    使用,。尽管 Lua 在 print 中放置了一个制表符,但 Lua 和 Python 都是一样的:

    print(2, 3) # 2   3
    

    或使用io.write,但您需要处理换行符。

    io.write("hello", " world\n") # hello world
    

    【讨论】:

      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多