有没有办法在数组元素中使用if语句?

时间:2019-08-28 15:51:54

标签: arrays batch-file if-statement

因此,我正在批量设置随机生成的世界。为此,我需要将if语句放入数组中。但是,似乎没有意识到我正在尝试将变量放入变量中。

到目前为止,我已经尝试使用!,%%和%作为变量。我还尝试删除了if语句,尽管错误停止了,但数字仍然低于0和高于15。

这是我当前拥有的代码。我认为问题出在第8和9行的2 if语句中,而没有它们

echo Generating biomes...
rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem The rest!
for %%x in (neg1 0 1) do (
   for %%y in (neg1 0 1) do (
      set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum[%%x][%%y] = biomenum[0][0] + nextbiomenum %% 2
      if %%biomenum[%x%][%y%]%% LSS 0 set %%biomenum[%x%][%y%]%% EQU "0"
      if %%biomenum[%x%][%y%]%% GTR 15 set %%biomenum[%x%][%y%]%% EQU "15" 
   )
)
echo %biomenum[neg1][neg1]% is the biome for -1,-1
echo %biomenum[neg1][0]% is the biome for -1, 0
echo %biomenum[neg1][1]% is the biome for -1, 1
echo %biomenum[0][neg1]% is the biome for 0, -1
echo %biomenum[0][0]% is the biome for 0, 0 -the starting chunk-
echo %biomenum[0][1]% is the biome for 0, 1
echo %biomenum[1][neg1]% is the biome for 1, -1
echo %biomenum[1][0]% is the biome for 1, 0
echo %biomenum[1][1]% is the biome for 1, 1

它应该做的是将数字保持在0到15之间,但是有时它会变成-1和16。它还会打印出Environment variable %biomenum[][]% EQU "15" not defined 9次。

1 个答案:

答案 0 :(得分:1)

此代码包含我在评论中建议的修改内容...

@echo off
setlocal EnableDelayedExpansion

echo Generating biomes...
rem The starting (center) biome
set /A biomenum0x0y = %RANDOM% * 14 / 32768 + 1
rem All biomes (including [0][0])
for %%x in (neg1 0 1) do (
   for %%y in (neg1 0 1) do (
      set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum[%%x][%%y] = biomenum0x0y + nextbiomenum %% 2
   )
)
rem biomenum[0][0] is the starting biome: reset it
set "biomenum[0][0]=%biomenum0x0y%"


echo %biomenum[neg1][neg1]% is the biome for -1,-1
echo %biomenum[neg1][0]% is the biome for -1, 0
echo %biomenum[neg1][1]% is the biome for -1, 1
echo %biomenum[0][neg1]% is the biome for 0, -1
echo %biomenum[0][0]% is the biome for 0, 0 -the starting chunk-
echo %biomenum[0][1]% is the biome for 0, 1
echo %biomenum[1][neg1]% is the biome for 1, -1
echo %biomenum[1][0]% is the biome for 1, 0
echo %biomenum[1][1]% is the biome for 1, 1