批处理,随机化数字数组时获取错误,然后添加它们

时间:2013-11-30 20:55:47

标签: batch-file

好吧,我的批处理系统有问题,我正试图通过城镇模拟器创建一个批处理游戏(种类)我想做的一件事(问题)是“宣传“你的城市,主要是随机的”人“住在你的房子里,我的问题在于你在做广告时它只是变得非常奇怪......继承人的代码:

:3
if %houses% LSS 2 goto YCGT
cls
set /a  send=%hutpop%+%small_housepop%+%housepop%+%slight_housepop%+%medium_housepop%+%big_housepop%+%mansionpop%
set /a hutpop=%hutpop%+%random% %% %maxhutpop%
set /a small_housepop=%small_housepop%+%random% %% %maxsmall_housepop%
set /a housepop=%housepop%+%random% %% %maxhousepop%
set /a slight_housepop=%slight_housepop%+%random% %% %maxslight_housepop%
set /a medium_housepop=%medium_housepop%+%random% %% %maxmedium_housepop%
set /a big_housepop=%big_housepop%+%random% %% %maxbig_housepop%
set /a mansionpop=%mansionpop%+%random% %% %maxmansionpop%
set/a pop=!hutpop!+!small_housepop!+!housepop!+!slight_housepop!+!medium_housepop!+!big_housepop!+!mansionpop!
if %pop% GTR %maxpop% (
set /a pop=%pop%-%maxpop%
)
timeout /t 5 /nobreak >nul
set /a recieve=!pop!-!send!
echo You have gained %recieve%!
echo You now have %pop%
pause>nul
goto rstart  

接下来会发生什么事情要么过去......给我“不能除零”的错误,或者总是给我你得到的最大人口数量...... (这绝对不是所有人代码!!!现在这是一个相当远的游戏...我不会发布所有1000行代码...只是询问你是否需要一些东西)

1 个答案:

答案 0 :(得分:1)

您的除以零错误是由%max...pop%之一的值为0引起的。如果%max...pop%为空,则会出现missing operand错误。

您显然已调用delayedexpansion,否则!var!会在set /a中显示错误。它将在块语句之外工作(即有时在FOR循环或IF...ELSE...中使用的多行括号语句序列,但不是必需的。您的set /a pop=...也可以与%var%因为%var%的值未在块内更改。

我建议你开始使用内部子程序来简化你的任务。例如,我修改了一些代码来分配用于工作的值:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
:3
SET houses=10
set hutpop=12
SET small_housepop=12
SET housepop=12
SET slight_housepop=12
SET medium_housepop=12
SET big_housepop=12
SET mansionpop=12
set maxhutpop=100
SET maxsmall_housepop=0
if %houses% LSS 2 goto YCGT
REM cls
set /a  send=%hutpop%+%small_housepop%+%housepop%+%slight_housepop%+%medium_housepop%+%big_housepop%+%mansionpop%
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
CALL :adjust hutpop
set /a hutpop=%hutpop%+%random% %% %maxhutpop%
set /a small_housepop=%small_housepop%+%random% %% %maxsmall_housepop%
set /a housepop=%housepop%+%random% %% %maxhousepop%
set /a slight_housepop=%slight_housepop%+%random% %% %maxslight_housepop%
set /a medium_housepop=%medium_housepop%+%random% %% %maxmedium_housepop%
set /a big_housepop=%big_housepop%+%random% %% %maxbig_housepop%
set /a mansionpop=%mansionpop%+%random% %% %maxmansionpop%
set/a pop=!hutpop!+!small_housepop!+!housepop!+!slight_housepop!+!medium_housepop!+!big_housepop!+!mansionpop!
if %pop% GTR %maxpop% (
set /a pop=%pop%-%maxpop%
)
REM timeout /t 5 /nobreak >nul
set /a recieve=!pop!-!send!
echo You have gained %recieve%!
echo You now have %pop%
pause>nul
goto rstart 
:: increment population with limit
:adjust
CALL SET /a %1+=%RANDOM% %%%% %%max%1%%
CALL SET /a hpop=%1 - %%max%1%%
IF %hpop% gtr 0 CALL SET %1=%%max%1%%
CALL ECHO %1=%%%1%%
GOTO :EOF

请注意调用子例程CALL :adjust hutpop的{​​{1}}修改adjust的值,其限制为hutpop。我故意用了五次来说明maxhutpop如何调整但是最重要的。在您的情况下,您所需要的只是

hutpop

还要注意一个潜在的问题。由于您call :adjust hutpop call :adust small_housepop ... 正在运营,因此如果您的邮件delayedexpansion需要更改为You have gained %recieve%!,那么批量可能会尝试查找"变量"} You have gained %recieve%! You need another %need%!,很可能找不到它,因此替换了一个空字符串 - 这可能令人费解。


进一步20131201T0511Z

You need another %need%

这是一个小型独立版本,使用两个略有不同的@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION :3 set hutpop=12 SET small_housepop=17 set maxhutpop=100 SET maxsmall_housepop=70 set /a send=%hutpop%+%small_housepop% ECHO you had %send%=%hutpop%+%small_housepop% CALL :adjust hutpop CALL :adjust small_housepop set /a pop=%hutpop%+%small_housepop% set /a recieve=%pop%-%send% echo You have gained %recieve%! echo You now have %pop%=%hutpop%+%small_housepop% ECHO ====== with pop-increase MORE limited set hutpop=12 SET small_housepop=17 set maxhutpop=100 SET maxsmall_housepop=70 set /a send=%hutpop%+%small_housepop% ECHO you had %send%=%hutpop%+%small_housepop% CALL :adjust2 hutpop CALL :adjust2 small_housepop set /a pop=%hutpop%+%small_housepop% set /a recieve=%pop%-%send% echo You have gained %recieve%! echo You now have %pop%=%hutpop%+%small_housepop% goto :eof :adjust CALL SET /a %1+=%RANDOM% %%%% %%max%1%% CALL SET /a hpop=%1 - %%max%1%% IF %hpop% gtr 0 CALL SET %1=%%max%1%% CALL ECHO %1=%%%1%% GOTO :EOF :adjust2 CALL SET /a %1+=(%RANDOM% %%%% %%max%1%% / 8) CALL SET /a hpop=%1 - %%max%1%% IF %hpop% gtr 0 CALL SET %1=%%max%1%% CALL ECHO %1=%%%1%% GOTO :EOF 程序。这是一些典型的输出(当然,你的将会有所不同,因为使用了:adjust

%RANDOM%

现在你需要做的就是按照弹跳球加入你的其他住宅类型。