【发布时间】:2018-03-14 10:25:25
【问题描述】:
我是批处理文件的新手,对于对批处理文件有一点了解的人来说,这似乎是一个愚蠢的问题,但我无法使用我在 C++ 或其他编程中的方式实现预期的结果。我试图做的是将三个 1 随机存储到每个 2x2 数组的元素中。提前致谢。完整代码如下:
@echo off
setlocal EnableDelayedExpansion
rem ========Creating four 2x2 zero arrays
for /l %%z in (0,1,3) do (
for /l %%y in (0,1,1) do (
for /l %%x in (0,1,1) do (
set map[%%x][%%y][%%z]=0
)
)
)
rem ========Putting three 1s in elements randomly **(How do I shorten this part?)**
set /a count=3
:while0
set /a i=!Random!%%2
set /a j=!Random!%%2
set /a sth=map[!i!][!j!][0]
if !sth! EQU 0 (
set map[!i!][!j!][0]=1
set /a count-=1
)
if not !count! EQU 0 GOTO while0
set /a count=3
:while1
set /a i=!Random!%%2
set /a j=!Random!%%2
set /a sth=map[!i!][!j!][1]
if !sth! EQU 0 (
set map[!i!][!j!][1]=1
set /a count-=1
)
if not !count! EQU 0 GOTO while1
set /a count=3
:while2
set /a i=!Random!%%2
set /a j=!Random!%%2
set /a sth=map[!i!][!j!][2]
if !sth! EQU 0 (
set map[!i!][!j!][2]=1
set /a count-=1
)
if not !count! EQU 0 GOTO while2
set /a count=3
:while3
set /a i=!Random!%%2
set /a j=!Random!%%2
set /a sth=map[!i!][!j!][3]
if !sth! EQU 0 (
set map[!i!][!j!][3]=1
set /a count-=1
)
if not !count! EQU 0 GOTO while3
rem ========Result
echo !map[0][0][0]!!map[1][0][0]!
echo !map[0][1][0]!!map[1][1][0]!
echo.
echo.
echo !map[0][0][1]!!map[1][0][1]!
echo !map[0][1][1]!!map[1][1][1]!
echo.
echo.
echo !map[0][0][2]!!map[1][0][2]!
echo !map[0][1][2]!!map[1][1][2]!
echo.
echo.
echo !map[0][0][3]!!map[1][0][3]!
echo !map[0][1][3]!!map[1][1][3]!
echo.
echo.
pause
【问题讨论】:
标签: loops batch-file goto