变量未被替换

标签 variables lua conditional-statements default-value

我一直在尝试 Lua 语言,似乎除了这个变量之外,所有变量都与条件交互。可以看一下吗?

function Medicial()
    local varName = "Marcy"
    local varCondition = "Well"
    local varSCondition = "1"  -- 5 = Uncurable, 10 = Well, 15 = Unknown, 20 = Surgery, 25 = Post Surgery, 29 = Bar Surgery
    local varDoctors = "DefultValue"
    local varExStatus = "DefultValue"
    local payment = "You can afford this."
    local total = 400
    if varCondition == "Well" then
        varDoctors = "Dr. Pope, Dr.Roadmiller"
        varStatus = "Yes"
    end
    if varCondition == "Sick" then
        varDoctors = "Dr. Pope, Dr.Rosenhour, Surgeon Rossford"
        varStatus = "No"
    end
    if total > 1000 then
        payment = "You can not afford this."
    elseif total >= 1000 then
        payment = "You can affort this, but you will be broke."
    end
    if varSCondition == 1 then
        varExStatus = "Well"
    end
    if varSCondition == 5 then
        varExStatus = "Uncurable"
    end
    if varSCondition == 15 then
        varExStatus = "Unknown"
    end
    if varSCondition == "20" then
        varExStatus = "Surgery"
    end
    if varSCondition == "25" then
        varExStatus = "Post Surgery"
    end
    if varSCondition == "29" then
        varExStatus = "Bar Surgery"
    end
    print("-=Subject Reports=-\n");
    print("Subject: "..varName.."\nCurrent Condition: "..varCondition.." ("..varExStatus..")\nCurrent Doctors: "..varDoctors.."\nCurrently Recovering? "..varStatus);
    print(">> "..payment);
end

它打印:

-=Subject Reports=-

Subject: Marcy
Current Condition: Well (DefultValue)
Current Doctors: Dr. Pope, Dr.Roadmiller
Currently Recovering? Yes
You can afford this.

最佳答案

这部分已损坏

if total > 1000 then
    payment = "You can not afford this."
elseif total >= 1000 then
    payment = "You can affort this, but you will be broke."
end

我打赌你打算这样做:

if total < 1000 then
    payment = "You can not afford this."
elseif total == 1000 then
    payment = "You can affort this, but you will be broke."
end

关于 varSCondition:您的 varSCondition 是一个字符串(“1”),但有时您会将其与整数(不带“-符号”)进行比较,例如这里:

if varSCondition == 1 then

有时到 Strings ,例如这里:

if varSCondition == "20" then

所有这些都应该是字符串或整数,但不要混合使用它们。试试这个:

local varSCondition = 1  -- 5 = Uncurable, 10 = Well, 15 = Unknown, 20 = Surgery, 25 = Post Surgery, 29 = Bar Surgery
...
if varSCondition == 1 then
    varExStatus = "Well"
end
if varSCondition == 5 then
    varExStatus = "Uncurable"
end
if varSCondition == 15 then
    varExStatus = "Unknown"
end
if varSCondition == 20 then
    varExStatus = "Surgery"
end
if varSCondition == 25 then
    varExStatus = "Post Surgery"
end
if varSCondition == 29 then
    varExStatus = "Bar Surgery"
end

关于变量未被替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225846/

相关文章:

c - 将 C 变量链接到 Lua

c# - 条件委托(delegate)?

c# - 多个条件语句的 AND/OR (&&/||) 逻辑

c - Lua中返回C指针时返回lua_integer类型而不是lightuserdata可以吗?

character-encoding - Lua String 字符编码

javascript if 条件检查带换行符的字符串,即\n

javascript - 在哪里调用 session_unset(user)

Javascript创建函数以在其他函数之间共享变量

php - 错误 “Undefined variable”

angular - 组件声明、模板变量声明和元素引用不包含此类成员