首页 > 解决方案 > 带有 if 语句的多个可能的文本输入

问题描述

我正在尝试使用批处理文件进行完全身临其境的文本冒险。

这是我的问题:我希望答案是文本输入,以便玩家输入指示他们要去哪里的响应。

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

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

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

: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

)

标签: batch-fileif-statement

解决方案


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

@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

推荐阅读