Batch file &引用;“Goto在这个时候出人意料”;

Batch file &引用;“Goto在这个时候出人意料”;,batch-file,Batch File,我正在批量制作一个战争战略游戏。这个想法是你选择你的国家,然后攻击另一个随机选择的国家。有三种武器:核武器、空袭和导弹。当我选择武器时,有时它会显示“goto此时出乎意料”并退出。我不知道该怎么办。它使用外部命令“batbox.exe” 你做了什么调试?请你链接batbox.exe,或者至少告诉我们它是做什么的,这样我们就可以测试你的代码了。初始调试:删除/注释@echo off,这样你就可以看到作为执行语句的语句,并希望能了解一下在哪里调用了有问题的goto。如有必要,添加echo COUNT

我正在批量制作一个战争战略游戏。这个想法是你选择你的国家,然后攻击另一个随机选择的国家。有三种武器:核武器、空袭和导弹。当我选择武器时,有时它会显示“goto此时出乎意料”并退出。我不知道该怎么办。它使用外部命令“batbox.exe”


你做了什么调试?请你链接
batbox.exe
,或者至少告诉我们它是做什么的,这样我们就可以测试你的代码了。初始调试:删除/注释
@echo off
,这样你就可以看到作为执行语句的语句,并希望能了解一下在哪里调用了有问题的
goto
。如有必要,添加
echo COUNTRY:%COUNTRY%:
键入命令以显示各个点的变量值(尾随的
有助于发现空变量和尾随空格`此外,批处理脚本中并非所有内容都必须左对齐。将代码缩进括号中的代码块中(
for
循环和
if
语句)以帮助提高一般可读性,并查看您是否具有与打开相同的关闭次数。但我怀疑问题在于,一个空格、符号或某些特殊字符被捕获到一个变量中,然后在检索时干扰
if
语句。您应该
使用
设置“variable=value”
“var=value”对被引用,如果“%var%”==“value”
比较,则引用所有
的每一面。好的,谢谢大家,我找到了解决方案。
@echo off
mode con: cols=80 lines=26
color a
title WW3
:menu
cls
echo                             World War 3
echo.
echo [Start Game]
echo [About]
echo [Exit]
for /f "delims=: tokens=1,2" %%A in ('batbox /m') do (
set x=%%A
set y=%%B
)
if %y%==2 (
if %x% geq 1 if %x% leq 12  (
goto setcountry
)
)
if %y%==3 (
if %x% geq 1 if %x% leq 7  (
goto about
)
)
if %y%==4 (
if %x% geq 1 if %x% leq 7  (
exit
)
)
goto menu
:setcountry
cls
echo Select your country.
echo.
echo [USA]
echo [Russia]
echo [Canada]
echo [UK]
for /f "delims=: tokens=1,2" %%A in ('batbox /m') do (
set x=%%A
set y=%%B
)
if %y%==2 (
if %x% geq 1 if %x% leq 5  (
set country=USA
goto setcpucountry
)
)
if %y%==3 (
if %x% geq 1 if %x% leq 8  (
set country=Russia
goto setcpucountry
)
)
if %y%==4 (
if %x% geq 1 if %x% leq 9  (
set country=Canada
goto setcpucountry
)
)
if %y%==5 (
if %x% geq 1 if %x% leq 4  (
set country=UK
goto setcpucountry
)
)
goto setcountry
:setcpucountry
set /a cpucountryran=%random% %%4 + 1
if %cpucountryran%==1 set enemy=USA
if %cpucountryran%==2 set enemy=Russia
if %cpucountryran%==3 set enemy=Canada
if %cpucountryran%==4 set enemy=UK
if %enemy%==%country% goto setcpucountry
goto setammo
:setammo
set missiles=3
set airstrike=2
set nuke=1
set health=1000
set cpumissiles=3
set cpuairstrike=2
set cpunuke=1
set cpuhealth=1000
:play
cls
echo %country%                                                                   %enemy%
echo HP: %health%                                                              HP: %cpuhealth%
echo.
echo [Nuke x %nuke%]                                                          [Nuke x %cpunuke%]
echo [Airstrike x %airstrike%]                                                [Airstrike x %cpuairstrike%]
echo [Missiles x %missiles%]                                                  [Missiles x %cpumissiles%]
for /f "delims=: tokens=1,2" %%A in ('batbox /m') do (
set x=%%A
set y=%%B
)
if %y%==3 (
if %x% geq 1 if %x% leq 9  (
goto nuke
)
)
if %y%==4 (
if %x% geq 1 if %x% leq 14  (
goto airstrike
)
)
if %y%==5 (
if %x% geq 1 if %x% leq 13  (
goto missiles
)
)
goto play
:nuke
if %nuke%==0 (
echo You are out of Nukes!
ping localhost -n 2 >nul
goto play
)
cls
echo %country% shot a Nuke on %enemy%!
echo.
set /a hit=%random% %%2 + 1
if %hit%==1 (
echo Miss!
)
if %hit%==2 (
echo Hit!
echo.
echo %enemy% lost 400 HP!
set /a cpuhealth=%cpuhealth%-400
set /a nuke=%nuke%-1
)
ping localhost -n 3 >nul
goto cpuplay
:airstrike
if %airstrike%==0 (
echo You are out of Airstrikes!
ping localhost -n 2 >nul
goto play
)
cls
echo %country% shot a Airstrike on %enemy%!
echo.
set /a hit=%random% %%2 + 1
if %hit%==1 (
echo Miss!
)
if %hit%==2 (
echo Hit!
echo.
echo %enemy% lost 400 HP!
set /a cpuhealth=%cpuhealth%-200
set /a airstrike=%airstrike%-1
)
ping localhost -n 3 >nul
goto cpuplay
:missiles
if %missiles%==0 (
echo You are out of Missiles!
ping localhost -n 2 >nul
goto play
)
cls
echo %country% shot a Missile on %enemy%!
echo.
set /a hit=%random% %%2 + 1
if %hit%==1 echo Miss!
if %hit%==2 (
echo Hit!
echo.
echo %enemy% lost 100 HP!
set /a cpuhealth=%cpuhealth%-100
set /a airstrike=%airstrike%-1
ping localhost -n 3 >nul
)
ping localhost -n 3 >nul
goto cpuplay
:cpuplay
set /a cpuweapon=%random% %%3 + 1
if %cpuweapon% equ 1 goto cpunuke
if %cpuweapon% equ 2 goto cpuairstrike
if %cpuweapon% equ 3 goto cpumissile
goto cpuplay
:cpunuke
if %cpunuke% equ 0 goto cpuplay
cls
echo %enemy% shot a Nuke on %country%!
echo.
set /a cpuhit=%random% %%2 + 1
if %cpuhit%==1 (
echo Miss!
ping localhost -n 3 >nul
)
if %cpuhit%==2 (
echo Hit!
echo.
echo %country% lost 400 HP!
set /a health=%health%-400
set /a cpunuke=%cpunuke%-1
ping localhost -n 3 >nul
)
goto play
:cpuairstrike
if %cpuairstrike% equ 0 goto cpuplay
cls
echo %enemy% shot a Airstrike on %country%!
echo.
set /a cpuhit=%random% %%2 + 1
if %cpuhit%==1 (
echo Miss!
ping localhost -n 3 >nul
)
if %cpuhit%==2 (
echo Hit!
echo.
echo %country% lost 200 HP!
set /a health=%health%-200
set /a cpunuke=%cpunuke%-1
ping localhost -n 3 >nul
)
goto play
:cpumissile
if %cpumissile% equ 0 goto cpuplay
cls
echo %enemy% shot a Missile on %country%!
echo.
set /a cpuhit=%random% %%2 + 1
if %cpuhit%==1 (
echo Miss!
ping localhost -n 3 >nul
)
if %cpuhit%==2 (
echo Hit!
echo.
echo %country% lost 100 HP!
set /a health=%health%-100
set /a cpunuke=%cpunuke%-1
ping localhost -n 3 >nul
)
goto play