【问题标题】:Goto was unexpected at this time batch windows 7 starterGoto此时出乎意料批处理Windows 7启动器
【发布时间】:2012-04-04 06:16:32
【问题描述】:

此代码旨在类似于口袋妖怪战斗游戏的更简单版本。我只编写了攻击代码。我一直在进行彻底的测试,发现每当用户确认他们的攻击时都会出现错误消息(此时 Goto 是意外的)。警告!!代码长 96 行。最后,我会放问题部分,所以你可以跳过第一个大块。

@echo off
Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8
:Begin
CLS
Echo Bulbasur
Echo %H2%/14      /\       
Echo           (__) ___  
Echo           l __lo.ol 
Echo           l_\ l_\"  
Echo.        
Echo     _ 
Echo *  / \ 
Echo \\l  )
Echo  \\__l   Charmander
Echo             %H1%/20
Echo -Attack -Capture
Echo -Item   -Run 
Set /p Move=Action?
If %move%==Attack goto Attack
If %move%==Catpure goto capture
If %move%==Item goto Item
If %move%==Run Goto Run
Echo I'm sorry, Charmander can't do that. 
Pause
goto Begin
:Attack
ClS
Echo Attacks
Echo 1)Tackle
Echo 2)Growl
Echo 3)Ember
Echo 4)Scratch
Set /p attack=Which one?
If %attack%==Tackle goto Tackle
If %attack%==1 goto Tackle
If %attack%==Growl Goto Growl
If %attack%==2 goto Growl
If %attack%==Ember goto Ember
If %attack%==3 goto Ember
If %attack%==Scratch goto Scratch
If %attack%==4 goto Scratch
If %attack%==Cancel goto Begin
Echo I didn't get that
Goto Attack
:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle 
:Growl
CLS
Echo Growl lowers the opponents attack.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Status
If %acccept%==No goto Begin
Echo I didn't get that.
goto Growl
:Scratch
CLS
Echo Scratch hits the foe with a claw.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto  Scratch
:Ember
CLS
Echo Ember hits the opponent with a small fire.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Ember
:Combat
CLS
If NOT %attack%==Growl If NOT %attack%==2 set /a H2=%H2%-(%A1%^2/%D2%)
set /a H1=%H1%-(%A2%^2/%D1%)
goto Begin
:Status
CLS
Set /a A1=%A1%-1
goto Combat

问题领域:

:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle 

代码在这里很好,但是一旦我在这里,它就不需要 goto 命令。有谁可以修这个牛肉? (注意:Tackle 只是一个示例。所有攻击都不起作用。) 编辑:如果用户输入“是”、“否”、胡言乱语或什么都不输入,它仍然会传递相同的错误消息(此时 goto 是意外的)

【问题讨论】:

  • 我对否决票很好奇; asker 发布了代码,挑出了一个示例问题区域,甚至给出了错误消息。对我来说似乎是个好问题。只是对批处理文件不屑一顾吗?这不应该影响问题的好坏。
  • @WesleyPetrowski,可能是因为他发布了他的整个剧本。然后发布了有问题的代码的附加副本。他应该只发布有问题的代码。
  • If %move%==Catpure goto capture 我知道那只猫是纯洁的,但我认为这是一个错字^^

标签: windows-7 batch-file goto


【解决方案1】:

你必须把它放在引号中:

if "%accept%"=="yes" goto combat
if "%accept%"=="no" goto begin

或者如果你不想让它区分大小写:

if /i "%accept%"=="yes" goto combat
if /i "%accept%"=="no" goto begin

【讨论】:

  • 不是真的必须,提醒你,但你当然可以,这可能是解决问题的最普遍的方法(也是一种,出于某种原因我当时没有想到)。
【解决方案2】:

你的问题是这一行:

set /p accept=Yes/No?

不使用与这些相同的变量名:

If %acccept%==Yes goto Combat
If %acccept%==No goto Begin

上面的变量有“ccc”,但第一个只是“cc”

编辑

您好,用户 1205760;我有一些时间可以消磨,所以我采用了你的程序并把它缩小了一些。这是我的版本:

@echo off

Setlocal EnableDelayedExpansion
Set Actions=Attack Capture Item Run
Set Attacks=Tackle Growl Ember Scratch Cancel
Set i=0
For %%a in (%Attacks%) do set /A i+=1 & set Attack[!i!]=%%a

Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8

:Begin
:Cancel
CLS
Echo Bulbasur
Echo %H2%/14      /\       
Echo           (__) ___  
Echo           l __lo.ol 
Echo           l_\ l_\"  
Echo.        
Echo     _ 
Echo *  / \ 
Echo \\l  )
Echo  \\__l   Charmander
Echo             %H1%/20
Echo -Attack -Capture
Echo -Item   -Run 
Set /p Move=Action? 
For %%a in (%Actions%) do if /I %move%==%%a goto %move%
Echo I'm sorry, Charmander can't do that. 
Pause
goto Begin

:Attack
Cls
Echo Attacks
For %%a in (1 2 3 4) do echo %%a)!Attack[%%a]!
Set /p attack=Which one? 
for %%a in (1 2 3 4) do if %attack%==%%a set attack=!Attack[%%a]!
for %%a in (%Attacks%) do if /I %attack%==%%a goto %attack%
Echo I didn't get that
Pause
Goto Attack

:Tackle
call :Confirm Tackle Hits The opponent where it hurts. EVERYWHERE.
If %accept%==Yes goto Combat
goto Begin

:Growl
call :Confirm Growl lowers the opponents attack.
If %accept%==Yes goto Status
goto Begin

:Ember
call :Confirm Ember hits the opponent with a small fire.
If %accept%==Yes goto Combat
goto Begin

:Scratch
call :Confirm Scratch hits the foe with a claw.
If %accept%==Yes goto Combat
goto Begin

:Status
Set /A A1-=1
:Combat
If /I NOT %attack%==Growl set /A H2=H2-(A1^2/D2)
set /A H1=H1-(A2^2/D1)
goto Begin

:Confirm 
Cls
Echo %*
Echo Do you want to?
set /p accept=Yes/No? 
For %%a in (Yes No) do if /I %accept%==%%a exit /B
Echo I didn't get that.
Pause
goto Confirm

【讨论】:

    【解决方案3】:

    如果用户不输入任何内容,您的 If 行可能会评估为如下内容:

    …
    If ==Yes goto Combat
    If ==No goto Begin
    …
    

    语法不正确。我建议在set /p 命令之前使用一些默认值初始化accept:

    …
    set accept=default
    set /p accept=Yes/No?
    if …
    

    这样,如果用户只是按 Enter 键,accept 变量将保留 default 的值,随后的 if 将不会以错误告终。

    【讨论】:

    • 或在 IF ==YES 之前设置一个测试来专门测试 %accept% 是否为空 - 它可能看起来像 'IF .%accept%==。转到 xxxx ...'
    • 您还可以在您的项目周围使用引号进行比较,因此它们评估为 If ""=="Yes"。按照 Andriy 的建议使用默认值仍然是个好主意。
    【解决方案4】:

    对不起,
    这只是反对投票的问题。
    问的人只是不确定问题一定出在那个部分。

    例如

    set a=
    if %a%==1 echo yes
    

    如果我只是发布这一行:

    if %a%==1 echo yes
    

    那么每个人都知道问题出在哪里吗?


    记住对于变量,比如%abc%,最好和",[或者{一起使用,以免出现错误信息。


    例如

    set /p abc=
    

    用户什么也不输入。
    那么下一行应该是:

    if %abc%==1 echo Hi
    

    但它变成了:

    if ==1 echo Hi
    

    ,as "%abc%"==""
    但是有了“”,就会变成

    if ""=="1" echo Hi
    

    而“”不等于“1”。
    明白吗?


    编辑---

    如果您使用的是 Windows 7(或其他版本),您也可以试试这个:

    choice /c YN /n /m "Confirm? [Y^|N]
    

    ^ 只是逃避“管道”(|)。

    希望这对你有用!

    【讨论】:

    • 发生了什么事?我投了反对票,我的评论被取消了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多