我的变量工作不正常..但理论上他们应该..(批处理文件)



我正在一个批处理文件中制作一个小冒险游戏,到目前为止,游戏在一个数学没有给出它需要的东西的地方中断了。。。这是它断裂的地方的代码,我将给出具有相同变量的工作车间设置的代码

set /a "22pistolammo=22pistolammo-3"
pause
goto safehouselate

现在,对于似乎有效的商店(尽管如此(:

set /p gunstore=
if %gunstore% == 1 set /a "22silencedpistol=22silencedpistol+1"
if %gunstore% == 1 set /a "money=money-450"
if %gunstore% == 2 set /a "22pistolammo=22pistolammo+10"
if %gunstore% == 2 set /a "money=money-10
if %money% lss 0 goto gunstoredeath
if %gunstore% == 3 goto fargomarketstreet
goto gunstore
set "_money=500"
set "_22pistolammo=500"
set /a "_22pistolammo-=3"
set "_gunstore=0"
set /p "_gunstore="
if %_gunstore% == 1 set /a "_22silencedpistol+=1"
if %_gunstore% == 1 set /a "_money-=450"
if %_gunstore% == 2 set /a "_22pistolammo+=10"
if %_gunstore% == 2 set /a "_money-=10
if %_money% lss 0 echo goto gunstoredeath
if %_gunstore% == 3 echo goto fargomarketstreet
echo goto gunstore
set _

这与问题代码类似。变量以下划线开头以避免变量以一个可能无效的数字开头,以及对于这些示例,易于由CCD_ 1输出。

问题:如果您输入空格或输入提示处的其他一些特殊字符。


set "_money=500"
set "_22pistolammo=500"
set /a "_22pistolammo-=3"
choice /c 123 /N
if errorlevel 1 (set /a "_22silencedpistol+=1" & set /a "_money-=450")
if errorlevel 2 (set /a "_22pistolammo+=10" & set /a "_money-=10")
if %_money% lss 0 echo goto gunstoredeath
if errorlevel 3 echo goto fargomarketstreet
echo goto gunstore
set _

这是使用if errorlevel。如果在提示下输入CCD_ 3,由于if errorlevel 1为1或更大,这是真的,if errorlevel 2是2或更大,这是真的,并且if errorlevel 3是3或更大,这是同样正确。

问题:有缺陷。


要使用if errorlevel进行检查,您可能需要从从最高到最低。

可以尝试这样的块:

if errorlevel 3 (
echo goto fargomarketstreet
) else if errorlevel 2 (
set /a "_22pistolammo+=10"
set /a "_money-=10"
) else if errorlevel 1 (
set /a "_22silencedpistol+=1"
set /a "_money-=450"
)

这可以工作,除了原始序列是校验1,检查2,检查钱,然后检查3。如果你被迫先检查3,然后检查需要检查的钱在检查3之前,检查1为时过早(用于调节货币(和支票2(用于调节金钱(。

问题:有缺陷。


使用if %errorlevel%而不是if errorlevel可以得到类似的结果第一个代码的行为:

set "_money=500"
set "_22pistolammo=500"
set /a "_22pistolammo-=3"
choice /c 123 /N
if %errorlevel% equ 1 (
set /a "_22silencedpistol+=1"
set /a "_money-=450"
) else if %errorlevel% equ 2 (
set /a "_22pistolammo+=10"
set /a "_money-=10"
)
if %_money% lss 0 echo goto gunstoredeath
if %errorlevel% equ 3 echo goto fargomarketstreet
if %_money% lss 0 echo goto gunstoredeath
echo goto gunstore
set _

检查1,否则检查2,检查钱,然后检查3。

问题:未知。(除了set _0在Windows XP中不存在(。


注意

以上所有代码都通过使用前导CCD_ 12来禁用CCD_,因为该代码没有针对CCD_ 13的标签。

校验3可以是指用3等的值来校验一个值。我想避免用冗长重复的术语。

建议使用第一个代码(使用set /p(或最后(使用choice(。

变量不能以数字开头,如%22silencedpistol%将被解析为%2,这是第二个(脚本|称为标签(参数,然后解析2silencedpistol%,这可能导致语法错误。解析器不知道%22silencedpistol%是一个变量,并满足%2的延迟匹配认可第一名。

一些更简单的代码可以修复您的代码:

set /a "_22pistolammo-=3"
pause
goto safehouselate
choice /c:123 /N
if %errorlevel% EQU 1 ( set /a "_22silencedpistol+=1" && set /a "money-=450" )
if %errorlevel% EQU 2 ( set /a "_22pistolammo+=10" && set /a "money-=10" )
if %money% lss 0 goto gunstoredeath
if %errorlevel% EQU 3 goto fargomarketstreet
goto gunstore

set /a中的缩短变量。替换了包含数字的变量。请在新的命令窗口中按set /?阅读set /a的帮助页面

最新更新