批处理文件游戏出现问题,缺少操作员错误



我已经开发批处理文件游戏一段时间了,但我正在制作的商店遇到了麻烦。当我运行脚本并试图在商店里购买东西时,它会给我"缺少操作员",然后显示购买物品的消息,但不会将物品添加到玩家的库存中。这是代码。非常感谢您的帮助。

:w_shop
title Nova: In the Weapon Shop
:sword_screen
cls
echo.
echo You have %money% gold.
echo.
echo What will you buy?
echo 1) Wooden Sword: 500 gold
echo You own %sword1%
echo.
echo 2) Stone Sword: 1000 gold
echo You own %sword2%
echo.
echo 3) Iron Blade: 5000 gold
echo You own %sword3%
echo.
echo 4) Mythril Sabre: 7500 gold
echo You own %sword4%
echo.
echo 5) Mythril Longsword: 10000 gold
echo You own %sword5%
echo.
echo 6) Next
echo.
echo 7) Leave Shop
set/p sword_screen=
if %sword_screen% LEQ 0 goto sword_screen
if %sword_screen% GEQ 8 goto sword_screen
if %sword_screen% EQU 1 goto buy_sword1
if %sword_screen% EQU 2 goto buy_sword2
if %sword_screen% EQU 3 goto buy_sword3
if %sword_screen% EQU 4 goto buy_sword4
if %sword_screen% EQU 5 goto buy_sword5
if %sword_screen% EQU 6 goto gauntlet_screen
if %sword_screen% EQU 7 goto main_menu
:buy_sword1
set price=500
set att=100
if %money% LSS %price% goto lack_funds
set/a money=%money%-%price%
set/a %sword1%=%sword1%+1
echo.
echo You bought a Wooden Sword. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword2
set  price=1000
set  att=150
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword2%=%sword2%+1
echo.
echo You bought a Stone Sword. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword3
set price=5000
set att=300
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword3%=%sword3%+1
echo.
echo You bought a Iron Blade. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword4
set price=7500
set att=500
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword4%=%sword4%+1
echo.
echo You bought a Mythril Sabre. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword5
set /a price=10000
set /a att=1000
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword5%=%sword5%+1
echo.
echo You bought a Mythril Longsword. This weapon has %att% attack.
pause>nul
goto sword_screen

需要明确的是,剑变量和货币变量是在代码

中早些时候声明的

由于您既没有定义货币也没有定义剑变量,因此行

if %money% LSS %price% goto lack_funds
set/a %sword1%=%sword1%+1

扩展到

if  LSS 500 goto lack_funds
set/a =+1

这是错误的。这些都是常见的错误。把线路改成这样:

if [%money%] LSS [%price%] goto lack_funds
set/a sword1=0%sword1%+1

请注意,变量被方括号包围,因此即使变量未定义,您也会得到至少一个有效的行

请注意,设置行中的左参数不能有%-符号,右参数有一个额外的0,因此如果变量未定义,您将获得0+1,这是有效的

顺便说一句,在你的代码片段中没有lack_funds标签,我也建议添加

echo off
set money=5000

在脚本的开头

最新更新