批处理文件 - '命令的语法不正确'

时间:2015-12-13 23:56:01

标签: batch-file cmd

我一直在进行文字冒险,但在编码结束时,它出现了这个错误。每当我尝试从场景5继续到场景5.1时,它会进入不同的场景并跳过它。帮助

这是我的代码:

cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1

cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1

:scene5.1
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
echo 1) Table
echo 2) Sofa
echo 3) Fireplace
echo 4) Go back
set /p type
if %type%==1 goto scene5.1.1
if %type%==2 goto scene5.1.2
if %type%==3 goto scene5.1.3
if %type%==4 goto scene5.1.4

2 个答案:

答案 0 :(得分:1)

C:\Windows\system32>echo 4) Go back
4) Go back

C:\Windows\system32>set /p type
The syntax of the command is incorrect.
goto was unexpected at this time.

set /p可能缺少等号(我知道它是)。

PS:Set /p var = PromptText不是最好的命令。它不会阻止错误输入,也不会检查它。请改用Choice命令。它只需要允许进入。

大多数示例使用set / p的原因是15年前从一个Windows版本中删除了选项。那是在不久前。

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Description:
    This tool allows users to select one item from a list
    of choices and returns the index of the selected choice.

Parameter List:
   /C    choices       Specifies the list of choices to be created.
                       Default list is "YN".

   /N                  Hides the list of choices in the prompt.
                       The message before the prompt is displayed
                       and the choices are still enabled.

   /CS                 Enables case-sensitive choices to be selected.
                       By default, the utility is case-insensitive.

   /T    timeout       The number of seconds to pause before a default
                       choice is made. Acceptable values are from 0 to
                       9999. If 0 is specified, there will be no pause
                       and the default choice is selected.

   /D    choice        Specifies the default choice after nnnn seconds.
                       Character must be in the set of choices specified
                       by /C option and must also specify nnnn with /T.

   /M    text          Specifies the message to be displayed before
                       the prompt. If not specified, the utility
                       displays only a prompt.

   /?                  Displays this help message.

   NOTE:
   The ERRORLEVEL environment variable is set to the index of the
   key that was selected from the set of choices. The first choice
   listed returns a value of 1, the second a value of 2, and so on.
   If the user presses a key that is not a valid choice, the tool
   sounds a warning beep. If tool detects an error condition,
   it returns an ERRORLEVEL value of 255. If the user presses
   CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
   of 0. When you use ERRORLEVEL parameters in a batch program, list
   them in decreasing order.

Examples:
   CHOICE /?
   CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
   CHOICE /T 10 /C ync /CS /D y
   CHOICE /C ab /M "Select a for option 1 and b for option 2."
   CHOICE /C ab /N /M "Select a for option 1 and b for option 2."

正如帮助所说,按降序使用错误级别

if errorlevel 4 dosomething
if errorlevel 3 dosomething
if errorlevel 2 dosomething
if errorlevel 1 dosomething
if errorlevel 0 UserPressedCtrl+C

或以任何顺序

if errorlevel 1 if not errorlevel 2 dosomething for 1

不要使用%errorlevel%,因为它可以被其他程序覆盖。

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL

:scene4.6.1
:scene5.1
SET "scene=5.1"
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
CALL :addchoice Table Sofa Fireplace "Go back"

GOTO makechoice

:scene5.1.1
ECHO AT table
goto :eof

:scene5.1.2
ECHO AT sofa
goto :eof

:scene5.1.3
ECHO AT fireplace
goto :eof

:scene5.1.4
:scene4.6
SET "scene=4.6"
echo You are IN a dank corridor dimly lit by sputtering torches
echo.
CALL :addchoice Doorway Corridor
GOTO makechoice


:makechoice
CHOICE /C %choices% 
SET "scene=%scene%.%errorlevel%"
SET /a choicecount=0
SET "choices="
GOTO scene%scene%

:addchoice
SET /a choicecount+=1
ECHO %choicecount%) %~1
SET "choices=%choices%%choicecount%"
SHIFT
IF "%~1" neq "" GOTO addchoice

GOTO :EOF

这可以为您节省一大堆编程。由于bgalea's回答实际上回答了您的问题,请不要接受它作为答案。这只是一种让构建冒险练习更快更容易的方法。

对于每个场景,描述场景并使用可用选项的参数调用:addchoice。如果选择是多个单词,“将它们括在引号中”。

例程将1分配给列表中的第一个选项,2分配给下一个选择,依此类推。然后将可用选项记录在choices

转到makechoice。这会使用choices中的列表提示输入。然后将dot + errorlevel追加到当前场景编号,清除选项并转到scene +计算出的场景编号。

请注意,您可以使用empy命令序列在场景之间移动,使用类似于scene5.1.4到scene4.6的结构(iow,场景5.1 + choice4移动到4.6)

这样,您的响应集在场景描述后变为一行,您永远不需要使用一系列if命令在场景之间移动。