string - 'format'的错误参数#3(预期字符串, bool 值)?

标签 string error-handling lua rainmeter

编辑雨量表皮肤的lua文件“您需要外套吗”
获取此代码标题中的错误

--[[ Given the current temperature, return the appropriate
  string for the main string meter ]]
local function getMainString( temp )
local negation = (temp > Settings.Ss_Limit) and " don't" or ""
local summerwear = (temp < Settings.Ss_Limit) and (temp > Settings.Vest_Limit) and "shirt and shorts"
local innerwear = (temp < Settings.Vest_Limit) and (temp > Settings.Jacket_Limit) and "vest"
local southerwear = (temp < Settings.Jacket_Limit) and (temp > Settings.Coat_Limit) and "jacket"
local outerwear = (temp < Settings.Coat_Limit) and "coat"
return string.format("You%s need a %s", negation, (summerwear or innerwear or southerwear or outerwear))
end

应该根据温度给正确的衣服。我曾尝试在不同的位置进行温度变化,而唯一的错误是温度超过Ss_limit。我没有太多的编码经验,所以请先谢谢

最佳答案

temp大于Settings.Ss_Limit或等于Settings.*_Limit中的任何一个时,所有summerwearinnerwearsoutherwearcoatwear均为false。这会使(summerwear or innerwear or southerwear or outerwear)成为false( bool(boolean) 值),而不是引起错误的字符串。

可能的解决方法:

--[[ Given the current temperature, return the appropriate
  string for the main string meter ]]
local function getMainString( temp )
local negation = (temp > Settings.Ss_Limit) and " don't" or ""
--[[ this is used to produce "You don't need a cloth" when
    temp is greater than Ss_Limit. Adjust the string based on your own need.
]]
local clothwear = (temp > Settings.Ss_Limit) and "cloth"
--[[ changed < to <=, the following is the same, as not to get an error
  when temp equals any of the _Limit .
]]
local summerwear = (temp <= Settings.Ss_Limit) and (temp > Settings.Vest_Limit) and "shirt and shorts"
local innerwear = (temp <= Settings.Vest_Limit) and (temp > Settings.Jacket_Limit) and "vest"
local southerwear = (temp <= Settings.Jacket_Limit) and (temp > Settings.Coat_Limit) and "jacket"
local outerwear = (temp <= Settings.Coat_Limit) and "coat"
--[[ added clothwear here, to produce proper output 
  when temp is greater than Ss_Limit
]]
return string.format("You%s need a %s", negation, (clothwear or summerwear or innerwear or southerwear or outerwear))
end

关于string - 'format'的错误参数#3(预期字符串, bool 值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056455/

相关文章:

PHP - 检查数组的所有元素是否都在字符串中

JavaScript:使用 Join 使数组成为字符串,使用输入参数作为分隔符 Btw Words --> Replace Error

c - 为什么我可以将更长的字符串分配给 C 中的指针?

random - 从表中选择一个随机项目

c - 函数 shmdt() 有什么问题?

exception - 如何在 Octave 的.oct文件中引发异常?

t-sql - 我是否必须在 T-SQL 中的 catch block 中回滚事务之前对事务进行计数?

python - 自定义回溯输出

c++ - Lua PANIC 错误

ssl - Lua:如何在使用 LuaSec 成功进行客户端身份验证后获取客户端详细信息