我刚开始学习批处理,并认为最好的学习方法就是给自己一个项目。我正在尝试一个基于文本的RPG,并挂在一个地方。我试图在一个“对话”中嵌套多个IF-IF NOT DEFINED和IF-ELSE“问题”。
if "%choice18%"=="talk hag" (
if "%glove%"=="0" (
if "%ketchup%"=="2" (
echo.
echo "AHH! Can it really be ye, Cramforth?" the hag asks. "If it be, then ye wouldn't mind answering a few personal questions bout us to prove it, would ye?" GULP! Art thou ready to answer the hag's personnal questions?
echo.
pause
:hag
echo.
echo Yes or no:
set /p choice19=
echo.
if "%choice19%"=="no" (
cls
echo.
echo "Then I'll never know for sure," sniffs the hag. You Dungeonheartbreaker.
echo.
pause
cls
goto West2a
)
if "%choice19%"=="yes" (
cls
echo.
echo "Very well. If ye answers questions three, then I'll know thou art me precious Cramforth."
echo.
pause
cls
echo Where didst we geaux on our honeymoon? A: Wensleydire, B: Rottenscab, C: Blood Area, or D: Zork?
echo.
set /p choice20=
echo.
if "%choice20%"=="b" (
cls
echo.
echo What did ye get me for me 114th birthday? A: Toad, B: Eyeball, C: N64 Paddle, D: Commemorative mug?
echo.
set /p choice21=
echo.
if "%choice21%"=="d" (
cls
echo.
echo How many hags didst we have together? 1, 2, 3, or 4?
echo.
set /p choice22=
echo.
if "%choice22%"=="3" (
cls
echo.
echo "Cramforth! Tis really ye!" mistaking thee for her dead husband. Ye suffer several prickly kisses from the hag which leave the odor of rat liquer on thy face. "No wonder ye died of a bloody head, ye forgot thy FLASK GETTING GLOVE before ye set out to vanquish that rogue dungeon."
echo.
pause
echo.
echo "Perhaps ye can still defeat that dungeon, even in thy new hotter, spectal form." Um, eww. "Here!" she says and hands ye a wicked FLASK GETTING GLOVE!
echo.
pause
set glove=1
goto West2a
) else (
cls
echo.
echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
echo.
pause
cls
goto HD
)
) else (
cls
echo.
echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
echo.
pause
cls
goto HD
)
) else (
cls
echo.
echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
echo.
pause
cls
goto HD
)
)
if not defined "%choice19%" (
cls
echo.
echo Just answer the hag 'yes' or 'no'. Please?
echo.
goto hag
)
) else (
echo.
echo "Away with ye," she shrieks, "I speaketh only to me dead husband who died of a bloody head!" She becomes abruptly silent and goes back to her landbeforetimeing. A bloody head, eh?
echo.
pause
cls
goto West2a
)
) else (
echo.
echo "Ye go show that dungeon who's lord, Cramforth! You da lord!"
echo.
pause
cls
goto West2a
)
)
我遇到的问题是,第一个用户输入变量set /p choice19=
在第一次忽略了IF命令。
示例:
:hag
echo.
echo Yes or no:
set /p choice19=
用户输入:是
忽略:if "%choice19%"=="yes" (
转到:if not defined "%choice19%" (
cls
echo.
echo Just answer the hag 'yes' or 'no'. Please?
echo.
goto hag
重定向回:
:hag
echo.
echo Yes or no:
set /p choice19=
用户再次输入:是
这次它遵循正确的命令继续“是”分支。
然而,这对于不会重定向回问题(即if "%choice20%"=="b" (
)并结束游戏的结果造成问题。
我回顾了一个类似的问题(Nested IF ( IF ( ... ) ELSE( .. ) ) statement in batch)但是,我注意到它与我的情况略有不同,因为我的变量是定义的。所以,我很茫然。有什么想法吗?
注意:我知道我可以在没有深度嵌套的情况下重构命令来解决问题,只需要GOTO来解决问题:LABELS。我只是想看看是什么导致了这个问题。