带有if语句的多个可能的文本输入

时间:2019-03-02 14:15:37

标签: batch-file if-statement

我正在尝试使用批处理文件进行完全沉浸式的文字冒险。

这是我的问题:我希望答案是文本输入,以便玩家输入指示他们将前往何处的响应。

对于很多问题,我需要有多个可能的输入。例如,当遇到一个敌人时,您可以做很多事情,但是我只能弄清楚如何识别一个输入。

换句话说,我希望系统接受用户输入并相应地执行操作。

到目前为止,这是我本节的代码:

:forest1

echo you awake in a forest, you do not know where you are or why you are there.

echo infront of you is a small goblin like creature

:recoil1

echo What do you do?

set /p answer=

if %answer%==run (

goto run1

) else (

if %answer%==attack (

goto attack1

) else ( 

if %answer%==befriend(

goto befriend1

) else (

if %answer%==scream(

goto scream1

) else ( 

if %answer%==dance (

goto dance1

) else (

echo Nothing happened

timeout /t 1

goto forest1

)

2 个答案:

答案 0 :(得分:1)

您应该这样修改您的工作方式:

choice

您会看到:这有点复杂;你错过了很多括号!

因此,使用@echo off rem Your code before the code you provided above ^^ :forest1 echo You awake in a forest, you do not know where you are or why you are there. echo In front of you is a small goblin like creature goto :recoil1 :recoil1 echo What do you do? Here is a list of options: echo r - run away echo a - attack the goblin echo b - be friend with the goblin echo s - scream echo d - dance echo n - do nothing choice /C:rabsdn /N if errorlevel 6 ( echo Nothing happened. timeout /t 1 goto :forest1 ) if errorlevel 5 goto :dance1 if errorlevel 4 goto :scream1 if errorlevel 3 goto :befriend1 if errorlevel 2 goto :attack1 if errorlevel 1 goto :run1 命令进行一些修改:

if

更清晰,更快,更易读,不是吗?

请注意:errorlevelif errorlevel n应该降序排列,因为errorlevel表示n是否 大于或等于 到{{1}}!

修改选项以更适合您。

答案 1 :(得分:1)

为什么不尝试在 if 循环中使用 for 来完成这项工作?

 
@echo off
:forest1
cls & echo/ & if defined answer set answer=<nul
echo/  you awake in a forest, you do not know where you are or why you are there.
echo/  infront of you is a small goblin like creature

:recoil1
set /p "answer= What do you do? "
for %%i in (run attack befriend scream dance) do if /i "%answer%" == "%%i" goto :%answer%1

echo/ Nothing happened
timeout /t 1 & goto forest1

:run1 
echo/ Here I'm in Label run1 & exit /b

:attack1
echo/ Here I'm in Label attack1 & exit /b

:befriend1 
echo/ Here I'm in Label befriend1 & exit /b

:scream1 
echo/ Here I'm in Label scream1 & exit /b

:dance1
echo/ Here I'm in Label dance1 & exit /b