Goto was unexpected at this time batch windows 7 s

2019-06-17 07:59发布

问题:

This code is designed to resemble a simpler version of the Pokemon battle gameplay. I've only coded in the attacks. I've been testing thoroughly, and found that an error message (Goto was unexpected at this time) whenever the user confirmed their attack. WARNING!! Code is 96 lines long. At the end, I'll put the problem section, so you can skip this first huge chunk.

@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

Problem Area:

: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 

The code gets here fine, but once I'm here, it doesn't expect the goto commands. Anyone can fix this beef? (Note: Tackle is just an example. None of the attacks work.) EDIT: If the user puts in "Yes","No",gibberish, or nothing, it still delivers the same error message (goto was unexpected at this time)

回答1:

You have to put it in quotes:

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

Or rather if you dont want to make it case sensitive:

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


回答2:

Your problem is that this line:

set /p accept=Yes/No?

does NOT use the same variable name that these ones:

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

Above variables have "ccc", but the first one just "cc"

EDIT

Hi, user1205760; I got some time to spend, so I take your program and made it somewhat smaller. This is my version:

@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 the user enters nothing, your If lines probably evaluate to something like this:

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

which is syntactically incorrect. I would suggest initialising accept before the set /p command with some default value:

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

That way, if the user just hits Enter, the accept variable will retain the default value, and the subsequent if will not end up in the error.



回答4:

Excuse me,
This is just a question to the downvotes.
The people who asks just IS NOT SURE must the problem be in THAT PART.

e.g.

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

If I just post this line:

if %a%==1 echo yes

Then WILL EVERYBODY KNOW WHAT'S THE PROBLEM?


Remember for variable, like %abc%, it's better to use it with ",[ or { so as to prevent error message.


e.g.

set /p abc=

and the user inputs nothing.
Then the next line should be:

if %abc%==1 echo Hi

But it became:

if ==1 echo Hi

,as "%abc%"==""
But with "", it will become

if ""=="1" echo Hi

And "" unequal to "1".
Understand?


EDIT---

If you're using Windows 7 ( or other versions ), you may also try this:

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

The ^ is just escaping the "pipe" (|).

Hope this will be useful to you!