Batch file 当我尝试使用if/else时,为什么cmd会关闭?

Batch file 当我尝试使用if/else时,为什么cmd会关闭?,batch-file,Batch File,所以我真的不太懂编码。 我正试图用批处理文件制作一个愚蠢的基于文本的小游戏,但我立即遇到了一个问题,在第一个提示给出答案后,cmd立即关闭。我搞不懂这个问题。 谢谢你的帮助 @echo off echo You are Ryan Manfold, an American astronaut and captain of the Mayflower II. echo The MFII will be the first manned space vessel to reach Mars. You

所以我真的不太懂编码。 我正试图用批处理文件制作一个愚蠢的基于文本的小游戏,但我立即遇到了一个问题,在第一个提示给出答案后,cmd立即关闭。我搞不懂这个问题。 谢谢你的帮助

@echo off
echo You are Ryan Manfold, an American astronaut and captain of the Mayflower II.
echo The MFII will be the first manned space vessel to reach Mars. You and your six crewmates are 7 months into your 13 month journey.
echo The ship is comprised of three large rings which spin to simulate gravity, and a cylindrical module that the rings connect to.
echo You are asleep in your cabin in Ring 1, near the head of the ship.
pause
cls
:start
echo You wake up in the dark and reach over to flick on the lights.
echo The digital clock on the wall reads 9:23 AM, work shift doesn't start until 10.
echo Do you get up now anyway and head to the mess hall or do you go back to bed?
echo 1. Go to mess hall
echo 2. go back to sleep
set /p wake="Which One?:"
if %wake%==1 (
pause
goto insomnia
)else(
pause
goto next
)
:next
if %wake%==2 (
goto meetingone
)else(
echo That input is invalid
echo please try again.
pause
goto start
)
:insomia
echo you walk out of your room.
pause
exit

括号和else之间需要空格:
)else(


此外,如果您打开命令提示符并运行批处理文件,您将能够看到错误
,否则(此时是意外的。
但是如果您双击该文件,它将在您看到错误之前退出。

您指的是一个不存在的函数。您指示bat转到“失眠”,但没有“失眠”函数只有一个“失眠症”函数。所以问题是输入错误。

cmd
对空格有点挑剔。如果/?,请阅读
的输出以了解正确的语法。(
)否则(
给出语法错误,导致脚本终止)。有关疑难解答,请不要按鼠标运行单击。打开一个
cmd
窗口并从该窗口手动运行。该窗口将保持打开状态,使您能够读取错误消息。请在开始时尝试
setlocal enabledelayedexpansion
,然后在开始时尝试
endlocal
end@cup:为什么?代码中没有需要延迟扩展的内容n、 你应该看看这个命令。它有自己的错误处理功能,不会接受无效的选择。如果你是新来的,那么就避免cmd的所有令人讨厌的遗留问题,学习PowerShell,而不是可以通过更正简单的打字错误来回答的问题应该被投票关闭或标记,因为它们对未来的vis没有帮助这里的itors…从技术上讲,只需要
)else(
;右括号后不需要立即使用分隔符。