批处理文件游戏麻烦,缺少运算符错误
Batch file game trouble, missing operator error
我从事批处理文件游戏已有一段时间了,但我在制作商店时遇到了问题。当我 运行 脚本并尝试在商店购买东西时,它会给我 "missing operator" 然后显示购买物品的消息,但不会将物品添加到玩家的库存中。继承人的代码。任何帮助深表感谢。
: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
需要说明的是,sword 变量和 money 变量在代码的前面声明过
因为你没有定义 money 和 sword 变量,所以行
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
请注意,变量被方括号括起来,因此即使变量未定义,您也至少会得到一个有效行
请注意,set-line 中的左参数不能有 % 符号,右参数有一个额外的 0,这样如果变量未定义,你将得到有效的 0+1
顺便说一句,您的代码片段中没有 lack_funds 标签,我还建议添加
echo off
set money=5000
在脚本开头
我从事批处理文件游戏已有一段时间了,但我在制作商店时遇到了问题。当我 运行 脚本并尝试在商店购买东西时,它会给我 "missing operator" 然后显示购买物品的消息,但不会将物品添加到玩家的库存中。继承人的代码。任何帮助深表感谢。
: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
需要说明的是,sword 变量和 money 变量在代码的前面声明过
因为你没有定义 money 和 sword 变量,所以行
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
请注意,变量被方括号括起来,因此即使变量未定义,您也至少会得到一个有效行
请注意,set-line 中的左参数不能有 % 符号,右参数有一个额外的 0,这样如果变量未定义,你将得到有效的 0+1
顺便说一句,您的代码片段中没有 lack_funds 标签,我还建议添加
echo off
set money=5000
在脚本开头