我正在尝试进行文本冒险,但我对如何使用if / else语句处理变量毫无头绪。可能在我的代码中有一些错误:
@ echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if %escape% = 1
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
else
goto dungeon
:dungeon
cls
echo well done.
pause
exit
答案 0 :(得分:0)
如果您的if
语句附带else
语句,则需要使用括号。其他必须也采用) else (
格式,如下所示:
@ echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if "%escape%"=="1" (
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
) else (
goto dungeon
)
exit /b
:dungeon
cls
echo well done.
pause
exit
为了美学目的,我删除了空白线;如果你真的想要它们,你可以把它们放回去。我还添加了一个exit /b
,因为有一个goto
后跟你要去的标签是不好的形式。 if
语句中的引号用于防止脚本在用户未输入任何内容时中断。