我可以将批量显示中的负值设为0吗?

时间:2014-01-14 05:53:23

标签: batch-file

我正在为桌面RPG编写商店生成器。我将它设置为根据您选择的阵营生成不同的内容,随机滚动以确定商店的效果(差,平均,良好,优秀),然后随机确定商店中的商品数量。

然而,要使某些项目或多或少地显示出来,我将其设置为说3d6-5箭头,这会导致最小值为-2,最多为13个箭头。自然地,商店不能有-2箭头。有没有办法能够为变量设置最小和最大范围,但是0是最低的,还是将任何负值显示为0?

1 个答案:

答案 0 :(得分:1)

if %val% lss 0 set /a val=0

是一种方式

这是一个更全面的版本:

@ECHO OFF
SETLOCAL

SET val=5
ECHO ==== :setlt =====
CALL :setlt val 3&CALL :showval %val% val
CALL :setlt val 6&CALL :showval %val% val
CALL :setlt val 5&CALL :showval %val% val
CALL :setlt val 3 27&CALL :showval %val% val
CALL :setlt val 6 1&CALL :showval %val% val
CALL :setlt val 5 11&CALL :showval %val% val
ECHO ==== :setgt =====
CALL :setgt val 3&CALL :showval %val% val
CALL :setgt val 6&CALL :showval %val% val
CALL :setgt val 5&CALL :showval %val% val
CALL :setgt val 3 27&CALL :showval %val% val
CALL :setgt val 6 1&CALL :showval %val% val
CALL :setgt val 5 11&CALL :showval %val% val
ECHO ==== :setra =====
CALL :setra val 3 8&CALL :showval %val% val
CALL :setra val 8 11&CALL :showval %val% val
CALL :setra val 2 4&CALL :showval %val% val
CALL :setra val 3 8 6&CALL :showval %val% val
CALL :setra val 8 11 6&CALL :showval %val% val
CALL :setra val 2 4 6&CALL :showval %val% val
CALL :setra val 3 8 6 7 &CALL :showval %val% val
CALL :setra val 8 11 6 7&CALL :showval %val% val
CALL :setra val 2 4 6 7&CALL :showval %val% val
CALL :setra val 3 8 "" 6&CALL :showval %val% val
CALL :setra val 8 11 "" 6&CALL :showval %val% val
CALL :setra val 2 4 "" 6&CALL :showval %val% val
ECHO ==== add some extras =====
SET val=20&CALL :disp val 1 8 "" "" "The strange die rolls "
SET val=-4&CALL :disp val 0 8 "" "" "" "ghouls flee in fear"
SET val=-4&CALL :disp val 0 8 "" "" "There are " "arrows in stock"
SET val=-4&CALL :dispgz val 0 8 "" "" "There are " "arrows in stock"
SET val=14&CALL :dispgz val 0 8 "" "" "There are " "arrows in stock"
GOTO :EOF

:: set a variable %1 to %3 if it is less than %2
:setlt
CALL SET $1=%%%1%%
IF %$1% lss %2 SET "%1=%~3"
IF NOT DEFINED %1 SET "%1=%~2"
goto :eof

:: set a variable %1 to %3 if it is greater than %2
:setgt
CALL SET $1=%%%1%%
IF %$1% gtr %2 SET "%1=%~3"
IF NOT DEFINED %1 SET "%1=%~2"
goto :eof

:: set a variable %1 to %4 if it is less than %2 or %5 if it is greater than %3
:setra
CALL ECHO if %%%1%% .lt. %2 SET to "%~4" IF %%%1%% .gt. %3 to "%~5"
CALL :setlt %1 %2 %~4
CALL :setgt %1 %3 %5
goto :eof

:showval
CALL ECHO value was %1 is now %%%2%%
SET %2=%1
GOTO :eof

:: Display with limits.
:disp
setlocal
CALL SET $2=%%%1%%
CALL :setra $2 %2 %3 "%~4" "%~5"
ECHO %~6%$2% %~7
endlocal
GOTO :eof

:: Display with limits - but only if resolved value is greater than zero
:dispgz
setlocal
CALL SET $2=%%%1%%
CALL :setra $2 %2 %3 "%~4" "%~5"
IF %$2% gtr 0 ECHO %~6%$2% %~7
endlocal
GOTO :eof

这是输出:

==== :setlt =====
value was 5 is now 5
value was 5 is now 6
value was 5 is now 5
value was 5 is now 5
value was 5 is now 1
value was 5 is now 5
==== :setgt =====
value was 5 is now 3
value was 5 is now 5
value was 5 is now 5
value was 5 is now 27
value was 5 is now 5
value was 5 is now 5
==== :setra =====
if 5 .lt. 3 SET to "" IF 5 .gt. 8 to ""
value was 5 is now 5
if 5 .lt. 8 SET to "" IF 5 .gt. 11 to ""
value was 5 is now 8
if 5 .lt. 2 SET to "" IF 5 .gt. 4 to ""
value was 5 is now 4
if 5 .lt. 3 SET to "6" IF 5 .gt. 8 to ""
value was 5 is now 5
if 5 .lt. 8 SET to "6" IF 5 .gt. 11 to ""
value was 5 is now 6
if 5 .lt. 2 SET to "6" IF 5 .gt. 4 to ""
value was 5 is now 4
if 5 .lt. 3 SET to "6" IF 5 .gt. 8 to "7"
value was 5 is now 5
if 5 .lt. 8 SET to "6" IF 5 .gt. 11 to "7"
value was 5 is now 6
if 5 .lt. 2 SET to "6" IF 5 .gt. 4 to "7"
value was 5 is now 7
if 5 .lt. 3 SET to "" IF 5 .gt. 8 to "6"
value was 5 is now 5
if 5 .lt. 8 SET to "" IF 5 .gt. 11 to "6"
value was 5 is now 8
if 5 .lt. 2 SET to "" IF 5 .gt. 4 to "6"
value was 5 is now 6
==== add some extras =====
if 20 .lt. 1 SET to "" IF 20 .gt. 8 to ""
The strange die rolls 8 
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
0 ghouls flee in fear
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
There are 0 arrows in stock
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
if 14 .lt. 0 SET to "" IF 14 .gt. 8 to ""
There are 8 arrows in stock

现在我可以随心所欲地摆弄......

val设置为5后,基本格式有很多行

CALL :setlt val 3&CALL :showval %val% val

例程:showval显示val的当前值,然后将其重置为原始值。这样,我可以报告前面CALL的结果并将值重置为我选择的任何值(但我没有使用该功能......)

:setlt调用的参数是variable,limit,forced_value - 其中forced_value是可选的。如果未指定强制值,则将使用限制。

如果:setlt参数为val 3,则val的值会将3的值设置为less than 3,如果其当前值为val 6 1 - 但参数为val {1}}然后,如果6的当前值小于1,则其值将更改为:showval

因此val例程会在每种情况下显示结果,然后将5重置为:setgt

greater than类似,只需设置值,如果当前值为less than而不是:setra

下一组是var minval maxval forceifltmin forceifgtmax,其中包含参数forceifltmin - 唯一的问题是,如果未使用forceiflmax,但使用了forceifltmin ""必须是空参数(call echo...)。

我在:setra的开头添加了REM行,纯粹是为了显示正在发生的事情 - 它对调试非常有用。只需::set "debug=REM " %debug%echo this is a debug line 该行,即可开启或关闭调试功能。

(提示:使用调试行,您可以尝试

debug

只需将REM设置为:disp或不设置任何内容即可关闭或打开调试行

最后是:dispgz"quoted"例程。这些略有不同。他们故意更改变量的值,并设计为显示值,就好像它在范围内一样,还有两个参数 - 可选的是text-before和text-价值之后。两者都应该提供text-before

这一次只显示text-after>0,因此您可以按照所示构建消息。

如果您不想显示包含值的行,除非应用范围检查后的值为:dispgz,请使用{{1}}