batch-file - 带有 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
)
解决方案
为什么不尝试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
推荐阅读
- mongodb - 使用凭据连接到 MongoDB - Micronaut
- reactjs - 如何创建一个接收依赖项的自定义钩子?
- node.js - 在 Botframework 中向 Directline API 响应添加变量
- python - 如何在 Django 中向 QuerySets 添加自定义属性?
- python - 从数据库中删除选定的树视图项目的问题
- android - 构建 AOSP 自定义 rom
- javascript - 如何使用 Javascript 对 HTML 表单进行错误检查,包括验证字段是否填写正确?
- python - 这些模型是否等效?
- mongodb - 查找数组中 NO 项具有值的所有记录
- c - fgets 在无限循环终止