IF / ELSE批量声明......?

时间:2016-10-24 17:20:09

标签: batch-file if-statement

Hello stackoverflow社区!首先,这是我的第一篇文章,我对技术术语不太好,所以请原谅这篇文章中的任何错误或类似的事情。

我正在批量制作基于文本的冒险游戏,而我目前正在处理其中的一小部分。对于我的生活,我只是无法弄清楚如何做IF / ELSE语句。我尝试过嵌套条件,但没有成功。这就是我所拥有的:

echo You step up to the light. Luckily, it's pretty dim,
echo so you're not burning your corneas.
echo:
echo What would you like to do?
echo:
echo 1] Attempt to fix the light
echo 2] Go back to do something else
echo:
set /p decision=I want to...
if "%decision%"=="2" goto restartStartNoLightFix
if "%decision%"=="1" goto 
echo That choice is invalid.
ping localhost -n 4 >nul
goto inspectFlickerLight

@echo已关闭。

我想要"%决定%" ==" 1"然后检查玩家是否有部分来修理灯光。如上所述,嵌套条件是失败的,并且由于这将是一个完整的游戏,似乎使用goto checkvariable将围绕我使用的每个决定。 此外,决策是一个不断变化的变量,如果这将影响任何事情。

我很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

在提问之前直接检查变量怎么样?

Echo=You step up to the light. Luckily, it's pretty dim,
Echo= so you're not burning your corneas.
Echo=
If Defined LightFix (
    Echo=What would you like to do?
    Echo=
    Echo=1] Attempt to fix the light
    Echo=2] Go back to do something else
    Echo=
    Set/P decision=I want to...
    If "!decision!"=="2" GoTo :restartStartNoLightFix
    If "!decision!"=="1" GoTo :inspectFlickerLight
    Echo=That choice is invalid.
    Ping localhost -n 4 >nul
)

如果变量在这种情况下名为%LightFix%用于演示目的,之前没有设置玩家获得该部分,那么他们就不会永远不会看到这个问题。

根据您的偏好,您也可以这样写:

Echo=You step up to the light. Luckily, it's pretty dim,
Echo= so you're not burning your corneas.
Echo=
If Not Defined LightFix (GoTo :NextBit)
Echo=What would you like to do?
Echo=
Echo=1] Attempt to fix the light
Echo=2] Go back to do something else
Echo=
Set/P decision=I want to...
If "%decision%"=="2" GoTo :restartStartNoLightFix
If "%decision%"=="1" GoTo :inspectFlickerLight
Echo=That choice is invalid.
Ping localhost -n 4 >nul
:NextBit

我在Echo,(我个人喜好)之后使用=,而不是使用空格或冒号。

<强> 以下是您重新构建的评论部分。

If "%decision%"=="1" (
    If Defined hasPart (
        If "%hasPart%"=="True" (
            GoTo :fixFlickerLight
        ) Else (
            Echo=You need the parts to fix the light!
            Timeout 3 >Nul
            GoTo :inspectFlickerLight
        )
    )
)
相关问题