4
@echo off
color 0a
title Horror Game
echo.
echo.
echo.
echo.
echo Welcome to the game
echo If you get scared
echo Feel free to leave
echo.
echo.
echo You are in a dark room.
echo It is cold.
echo All you hear is a scratching sound
echo near your feet.
echo What do you do?
echo.
echo.
echo 1.) Feel around you
echo 2.) Listen for anything else
set/p input = Command?
if %input% == "1" goto Feel
if %input% == "2" goto Listen
echo.
echo.
:Feel
echo You feel around and hear a growl.
echo As you realize the scratching was
echo on your leg.
echo. 
echo You remember nothing else.
pause
end

我正在尝试为 cmd 制作基于文本的游戏,每当我尝试输入响应时都会立即关闭,我几乎无法读出“此时 goto 出乎意料”

4

2 回答 2

7

You did not form your tests correctly. You need to enclose the variable in double-quotes:

if "%input%" == "1" goto Feel
if "%input%" == "2" goto Listen

Note that you need an extra condition after these to deal with the possibility that they didn't enter 1 or 2.

You should consider using the choice command instead of set /p. Type choice /? from the command prompt.

In future, run your script from the command prompt if you're having trouble with premature termination. That way the window won't close on you and you'll have plenty of time to read the error.

You can run a command prompt quickly by pressing Windows Key-R, typing cmd and pressing Enter

于 2013-11-11T03:51:34.277 回答
1

您应该删除命令之间input和命令=中的空格set,否则您实际上是在设置一个具有名称的变量input (末尾有一个空格)。这按预期工作:

echo 1.) Feel around you
echo 2.) Listen for anything else
set/p input=Command?
if %input% == "1" goto Feel
if %input% == "2" goto Listen
echo.
echo.
:Feel
echo You feel around and hear a growl.
echo As you realize the scratching was
echo on your leg.
echo. 
echo You remember nothing else.

但是,如果用户输入带有空格的输入,则如果没有引号%input%将产生错误。unexpected gotopaddy 建议使用选择命令可能是要走的路。

于 2013-11-11T04:06:53.810 回答