【问题标题】:How to decrement values in Batch? (CLOSED)如何批量递减值? (关闭)
【发布时间】:2017-09-27 19:59:04
【问题描述】:

所以,我一直在尝试批量制作经典的“三门”游戏,但遇到了一个问题。显然很难批量减少值?有人知道我能做什么吗?我把我的游戏主循环放在这篇文章下面。 有什么办法可以有效而紧凑地进行递减吗?谢谢!我在错误位置周围加上了引号。

    :loopdeloop
    color 07
    set /a num=%random% %%3 +1
    SET /p door=Pick a number between one and three: 
    if %door% EQU %num% (
    color 0a
    @echo Correct!
    timeout /t 2 /nobreak>nul
    SET /a score+=1
    )
    if %door% NEQ %num% (
    color 0c
    @echo Incorrect!

SET /a score-=1

    timeout /t 1 /nobreak>nul
    cls
    @echo Three Doors.
    @echo Behind two of these doors there are ghosts.
    @echo Behind one, there is freedom.
    @echo Which one will you take?
    title Three Doors - Score: %score%
    goto :loopdeloop

【问题讨论】:

  • 第二个IF 命令缺少右括号。您的IF 命令正在创建一个带括号的代码块。任何时候你需要在代码块中引用一个变量,你都需要使用延迟扩展。 ss64.com/nt/delayedexpansion.html

标签: batch-file decrement


【解决方案1】:

两个独立的 if 相互跟随另一个 IMO 没有意义。
请改用 else 子句。 (并使用适当的缩进)

:loopdeloop
color 07
set /a num=%random% %%3 +1
SET /p door=Pick a number between one and three: 
if %door% EQU %num% (
    color 0a
    @echo Correct!
    SET /a score+=1
) else (
    color 0c
    @echo Incorrect!
    SET /a score-=1
)
timeout /t 2 /nobreak>nul
cls
@echo Three Doors.
@echo Behind two of these doors there are ghosts.
@echo Behind one, there is freedom.
@echo Which one will you take?
title Three Doors - Score: %score%
goto :loopdeloop

【讨论】:

  • 好的,现在解决了我要问的问题,如何减少批处理脚本中的值?如果 %door% 不等于 %num%,我想将“分数”减一
  • 您已经使用语法SET /a score+=1 来增加。你觉得SET /a score-=1做了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 2016-07-16
  • 2015-05-22
  • 2016-04-03
  • 2015-03-14
相关资源
最近更新 更多