【问题标题】:Arithmetic in Batch? [closed]批量算术? [关闭]
【发布时间】:2016-07-16 04:03:56
【问题描述】:

我正在制作一款游戏,我有一个Health 变量来跟踪玩家在玩游戏时的生命值。有一次,玩家在罐头上咬了一口(我知道这很正常),损失了 2 点生命值。我正在尝试使用算术从“健康”变量中减去 2 HP(此时仍为 100),然后显示他们还剩下多少健康,但在我尝试删除 2 后它仍然显示 100 HP生命值。我做得对吗?

:Beans
call :colorEcho 7 "You decide you're going to have Canned Beans for     breakfast. Yum. You grab a knife from the kitchen counter and work the top off. You eat the beans straight from the can. Ouch! You cut your lip! It starts to    bleed."
Echo .
set /a "_Health=_Health-2"
Echo.
call :colorEcho C "# ALERT; You have lost 2 Health Points! You are now at %Health% HP!"
Echo.
pause
exit

会发生什么:

【问题讨论】:

  • 你试过没有"s吗?
  • 我猜这个代码片段在括号 () 内的一个块中;如果我是对的,你需要delayed expansion,或者你写set /A _Health-=2...
  • @i_am_jorf 是的,我尝试不使用"s,但得到相同的结果。
  • @aschipfl 这个块不在括号内,但是我尝试了`set /a _Health-=2,但还是不行。
  • 当您的变量命名为_Health 时,为什么要扩展%Health%?注意 - 您可以简化为 set /a _Health-=2

标签: batch-file math arithmetic-expressions integer-arithmetic


【解决方案1】:

好的,感谢@dbenham,我找到了答案。谢谢!

我搞砸了变量,正如 dbenham 指出的那样,当我在 set /a _Health-=2 中使用 _Health 时,我正在扩展 %Health%。我把它改成了set /a Health-=2,现在一切似乎都正常了。

【讨论】:

    猜你喜欢
    • 2010-09-28
    • 2014-04-30
    • 2018-01-09
    • 1970-01-01
    • 2018-07-06
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多