首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批处理文件-‘命令的语法不正确’

批处理文件-‘命令的语法不正确’
EN

Stack Overflow用户
提问于 2015-12-14 07:56:01
回答 2查看 1.2K关注 0票数 0

我一直在做一个文本冒险,但在我编码的最后,它出现了这个错误。每次我尝试从场景5继续到场景5.1时,它都会转到不同的场景并跳过它。帮助?

下面是我的代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2015-12-14 11:05:31

代码语言:javascript
复制
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版本中删除了选项。那是很久以前的事了。

代码语言:javascript
复制
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."

正如help所说的,按降序使用errorlevel

代码语言:javascript
复制
if errorlevel 4 dosomething
if errorlevel 3 dosomething
if errorlevel 2 dosomething
if errorlevel 1 dosomething
if errorlevel 0 UserPressedCtrl+C

或以任何顺序

代码语言:javascript
复制
if errorlevel 1 if not errorlevel 2 dosomething for 1

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

票数 1
EN

Stack Overflow用户

发布于 2015-12-16 07:08:20

代码语言:javascript
复制
@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 answer实际上回答了您的问题。这只是一种使构建冒险练习更快、更容易的方法。

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

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

转到makechoice。这将提示您使用choices中的列表输入条目。然后将响应的dot+the errorlevel附加到当前场景编号,清除选择并转到scene+计算的场景编号。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34257869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档