【问题标题】:Goto was unexpected at this time - batchGoto此时出乎意料 - 批处理
【发布时间】:2012-12-29 01:19:21
【问题描述】:

我正在尝试制作基于文本的批量游戏。但是刚开始写就遇到了一个从来没有遇到过的问题。

: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

当我输入 1 或 2 时,它显示错误“goto is unexpected at this time”我该如何解决?

【问题讨论】:

    标签: goto


    【解决方案1】:

    您的startchoice 设置不正确。使用 set /p 的替代语法,在那里您提供提示(并删除 startchoice 和赋值 (=) 运算符之间的空格 - 我认为这实际上是问题的原因,但您可以减少批处理文件如果您使用 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
    

    【讨论】:

      【解决方案2】:

      您需要在 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
      

      【讨论】:

        【解决方案3】:

        不要使用环境变量,试试这个:

        CHOICE
        IF %ERRORLEVEL% EQU 1 goto changes
        IF %ERRORLEVEL% EQU 2 goto startgame
        

        Choice 是一个允许您输入数字或 y/n 并返回错误代码的程序。 %ERRORLEVEL% 是一个保存程序最后一个错误代码的变量。

        您也可以进行其他比较。

        EQU  - equal 
        NEQ - not equal 
        LSS - less than 
        LEQ - less than or equal 
        GTR - greater than 
        GEQ - greater than or equal 
        

        【讨论】:

          【解决方案4】:

          可能出现故障的另一个原因是您没有正确包含 SET /P M= 变量... m 和等号之间不应有空格。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多