나는 배치 파일에 익숙하지 않다. 그리고 이것은 배치 파일에 대해 조금 알고있는 사람에게는 어리석은 질문이지만 나는 C++이나 다른 프로그래밍에서하는 것처럼 원하는 결과를 얻을 수 없다. 내가하려고했던 것은 2x2 배열 각각에 대해 요소에 세 개의 1을 무작위로 저장하는 것입니다. 미리 감사드립니다. 전체 코드는 다음과 같습니다 :이 경우 루프 또는 goto를 사용하여 코드를 어떻게 줄입니까?
@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
내가하려고 무엇보다 우선이 대답을 수락하지 마십시오 do는 3 개의 1을 각각의 2x2 배열에 무작위로 저장하여 12 개의 1이 있어야하지만 코드가 총 3 개의 1을 생성하는 것으로 보이며 컴파일하는 데 오랜 시간이 걸리는 것처럼 보입니다. – jacknip