Lua - 如何在一条 print 语句中打印两个内容

在 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) + "%")不能正常工作。

点赞
用户8150685
用户8150685

在 Lua 和 Python 中使用即可。但是,Lua 在 print 中它们之间放置一个制表符:

print(2, 3) # 2   3

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

io.write("hello", " world\n") # hello world
2019-10-16 16:27:21
用户2860267
用户2860267

如果你只是进行简单的字符串操作,你可以像这样使用 .. 连接它们:

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

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

local foo = 100
print( string.format("%d%%", foo)) -- 100%
2019-10-16 16:50:29