Goto此时出乎意料 - 批次

use*_*683 4 goto

我正在尝试制作基于文本的批处理游戏.但我遇到了一个问题,刚开始写它以前从未遇到过的.

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
echo enter your choice:
set /p startchoice =
if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame
Run Code Online (Sandbox Code Playgroud)

当我键入1或2时,它显示错误"此时goto意外"我该如何解决?

Ken*_*ite 7

您的startchoice设置不正确.使用备用语法在set /p那里提供提示(并删除之间的空间startchoice和assignment(=)运算符 - 我认为这实际上是问题的原因,但如果使用set /p <variable>=<Prompt>语法,可以通过一行减少批处理文件) .

我为gotoecho语句添加了两个目标,以便您可以看到它们已到达:

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
set /p startchoice=Enter your choice:

if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame
:changes
echo Changes
goto end
:startgame
echo StartGame
:end
Run Code Online (Sandbox Code Playgroud)


Pet*_*erJ 6

您需要在 if 比较周围加上引号,并且它不喜欢您在没有提示的情况下使用 set / p 。以下工作:

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
set /p startchoice = "enter your choice: "
if "%startchoice%"=="1" goto changes
if "%startchoice%"=="2" goto startgame
Run Code Online (Sandbox Code Playgroud)