括号if语句后批处理文件中的goto函数无法正常工作

时间:2015-02-16 23:50:31

标签: windows batch-file cmd

所以我试图让它成为如果玩家按下随机键而不是它会回到最后一个标签。如果你按一个键然后按回车键就可以了,但是如果你只按下输入而不是它关闭。请帮忙:

    @echo off
title dragon game
setlocal enabledelayedexpansion

:main
cls
color 07
echo Welcome to the Dragon's Journey Game
echo ------------------------------------
echo Please select an option:
echo.
echo 1)New Game
echo 2)Load
echo 3)About This Game
echo 4)Exit
set /p c=)                

if "%c%" == "1" goto new
if "%c%" == "2" goto load
if "%c%" == "3" goto about
if "%c%" == "4" goto exit
goto main

:about
cls
color 07
echo  ##############################################
echo ##This game was made purely for fun. So if you##
echo ##complain about it being too bad, well try to##
echo ##make a better one, because I don't have time##
echo ##to deal with you right now. Sayonara!  -Greg##
echo  ##############################################
pause
goto main

:exit
echo Are you sure you want to exit? (Y/N)
echo -----------------------------------
echo set /p c=)

if "%c%" == "y" exit
if "%c%" == "n" goto main
goto exit

:new
set strength=50
set intelligence=50
set infamy=50
set incognito=50
set stubbornness=50
set patience=50
set maneuverability=50
set flames=50
set brutality=50
set patience=50
set cap=100
set maturity=youth
set lair=none
goto name

:name
cls
color 07
echo Before you begin, please enter your name:
echo -----------------------------------------
set /p name=
goto surename

:surename
cls
color 07
echo Are you ok with the name, %name%? (Y/N)
echo ---------------------------------------
set /p c=)

if "%c%" == "y" goto pre
if "%c%" == "n" goto name
goto surename

:pre
cls
color 07
echo Prepare to enter a fantastical world full of werms and serpents!
echo ----------------------------------------------------------------
echo Are you sure you are prepared, %name%? (Y/N)
echo ----------------------------------------------------------------
set /p c=)

if "%c%" == "y" goto backstory
if "%c%" == "n" goto name
goto pre

:backstory
cls
color 80
echo As a child, you had several siblings, all of whom 
echo you competed with for your mother's favor.
echo.
echo Did you pursue a life of hunting, or a more 
echo subtle life of reading and gaining knowledge.
echo --------------------------------------------------
echo 1)Hunting
echo 2)Reading
echo 3)I did my fair share of both
set /p c=)

if "%c%" == "1" (
set strength=75
set intelligence=25
goto youth
)
if "%c%" == "2" (
set strength=25
set intelligence=75
goto youth
)
if "%c%" == "3" goto youth
goto backstory

:youth
cls
echo Good, good.
echo.
echo You are now a youth dragon who must fend for 
echo himself, after your mother died during a raid.
echo.
echo You are resting in a cavern for the time being.
echo However, you are cramped with little space for
echo comfort. You venture out to seek a new lair.
echo.
echo Which direction do you go?
echo -----------------------------------------------
echo 1)North
echo 2)South
echo 3)East
echo 4)West
set /p c=)

if "%c%" == "1" (
set lair=north1
goto northlair1
)
if "%c%" == "2" (
set lair=south1
goto southlair1
)
if "%c%" == "3" (
set lair=east1
goto eastlair1
)
if "%c%" == "4" (
set lair=west1
goto westlair1
)
goto youth

问题在于最后一个标签

2 个答案:

答案 0 :(得分:0)

set /p c=)

关闭时没有打开支撑。

Choice /c 1234 /n 
if errorlevel 1 if not errorlevel 2 echo 1 chosen

答案 1 :(得分:0)

如果您只是按 Enter ,则假设set /p var=promptvar设置为 no ,但事实并非如此。它将保持var不变,因此它将保留最后一个条目,因此退出到* lair1(未定义)

要确保您期望的行为,请使用

set "c="
set /p c=)

请注意,此特性可用于设置默认值

set c=6
set /p c=)
如果单独使用 Enter

c保留为6

尝试阅读call命令。

call :getresponse

将执行子程序":getresponse"可能是

:getresponse
set "c=%~1"
set /p C=)
goto :eof

提供您的预期行为,

call :getresponse defaultvalue

会获得响应但允许您单独设置 Enter 的默认值。