我的变量无法正常运行...但是理论上它们应该...(批处理文件)

时间:2018-12-20 15:07:53

标签: batch-file

我正在用批处理文件制作一个小型冒险游戏,到目前为止,该游戏在某一时刻中断,而数学只是无法给出所需的内容。这是中断的代码,我会给出代码,说明应该是具有相同变量的工作车间设置

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

2 个答案:

答案 0 :(得分:0)

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

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中变短的变量。替换的变量包含数字。请通过在新的cmd窗口中按set /a阅读set /?的帮助页面

答案 1 :(得分:0)

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 _

这类似于问题代码。 变量以下划线开头,以避免变量 以数字开头,该数字可能无效,并且 这些示例很容易由set _输出。

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


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。如果在提示符下输入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。

问题:未知。 (除了choice在Windows XP中不存在)。


注意

上面的所有代码都使用前导goto禁用了echo, 因为代码中没有goto的标签。

Check 3表示检查值为3等的值。 我想避免使用 长期重复条款。

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

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