【问题标题】:Batch : Simply Syntax Error : Goto was unexpected at this time批处理:简单语法错误:此时转到意外
【发布时间】:2013-10-10 23:58:52
【问题描述】:

所以我正在尝试编写一个基本的计算机程序。而且我在某些语法上遇到了一些麻烦。代码如下。

@echo off
cls
color 71
:start
echo        -----------------------------------
echo        Welcome to Tandy's Calculator! v0.1
echo        -----------------------------------
echo           Enter your first number below
echo Type "help" to see the list of available commands
echo        -----------------------------------
set /p "_input1=Please Enter The First Number or Command:"
cls
goto subroutine

:subroutine
:: the below line gives me the "goto was unexpected at this time" error
if /i "%_input1%"=="help" goto help1
if /i "%_input1%"=="back" goto _input1
if /i "%_input1%"=="clear" goto clearVar (
goto checkVar
)

每次执行文件时都会收到错误消息 “此时 goto 出乎意料”,命令窗口关闭。

使用暂停命令,我已经能够查明我收到错误消息的程序部分。程序中已对此进行了注释。

谁能帮助我?我的语法有什么问题?

【问题讨论】:

    标签: batch-file cmd goto


    【解决方案1】:

    试试这个:

    if /i "%_input1%"=="clear" goto clearVar
    goto checkVar
    

    这是无效的语法。

    if /i "%_input1%"=="clear" goto clearVar (
    goto checkVar
    )
    

    【讨论】:

    • 程序告诉我此时 goto 命令本身是意外的。这让我相信我搞砸了我的 if 命令语法。
    • goto clearVar (clear variable) 带我进入程序的这一部分:clearVar set "_input1=" :
    • 见我上面的编辑。还要确保 :clearVar 自己在一行中。
    【解决方案2】:

    设置变量似乎可以解决我遇到的问题。 我只是像这样在程序开始时初始化了变量

    ::command bank
    set help=help
    set back=back
    set clear=clear
    
    ::methods
    set add=add
    set subtract=subtract
    set multiply=multiply
    set divide=divide
    

    然后我将 if 语句更改为如下所示

    :subroutine2
    if /i "%action%"=="%help%" goto help2
    if /i "%action%"=="%back%" goto back2
    if /i "%action%"=="%clear%" goto clearVar
    if /i "%action%"=="%add%" goto _input2
    if /i "%action%"=="%subtract%" goto _input2
    if /i "%action%"=="%multiply%" goto _input2
    if /i "%action%"=="%divide%" (goto _input2) else (goto functionerror)
    

    这消除了我遇到的语法错误。问题似乎是它没有在“If”命令语法中识别 object2。将用户选项初始化为变量,然后为这些变量提供与用户在初始化命令时键入的完全相同的字符串,就成功了。

    如果有人想要完整的代码,以便他们可以进一步了解程序以及我的意思,请告诉我,我可以通过电子邮件发送/粘贴它以供将来参考。

    【讨论】:

      【解决方案3】:

      也试试这个:

      if /i "%_input1%"=="clean" (
          goto cleanVar
      ) ELSE (
          goto checkVar
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        相关资源
        最近更新 更多