goto此时出人意料

时间:2013-11-11 03:41:48

标签: batch-file goto

@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此时出乎意料"

2 个答案:

答案 0 :(得分:7)

您未正确形成测试。您需要将变量括在双引号中:

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

请注意,在这些之后你需要额外的条件来处理他们没有输入1或2的可能性。

您应该考虑使用choice命令而不是set /p。从命令提示符处键入choice /?

将来,如果您在提前终止时遇到问题,请从命令提示符运行脚本。这样窗口就不会关闭,你将有足够的时间来阅读错误。

您可以按 Windows Key - R 快速运行命令提示符,输入cmd并按 Enter

答案 1 :(得分:1)

您应该删除input命令中=set之间的空格,否则您实际上是在设置名为input 的变量(位置为{1}}结束)。这按预期工作:

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 goto错误。 paddy建议使用选择命令可能是要走的路。