【问题标题】:How do I shorten my code by using loops or goto in this case?在这种情况下,如何通过使用循环或 goto 来缩短我的代码?
【发布时间】: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


    【解决方案1】:
    @echo off
    setlocal EnableDelayedExpansion
    
    rem ========Creating four 2x2 one 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]=1
            )
        )
    )
    
    rem ========Putting one 0 in an element randomly
    for /l %%z in (0,1,3) do (
        set /a i=!Random!%%2
        set /a j=!Random!%%2
        set map[!i!][!j!][%%z]=0
    )
    
    rem ========Result
    for /l %%z in (0,1,3) do (
       echo !map[0][0][%%z]!!map[1][0][%%z]!
       echo !map[0][1][%%z]!!map[1][1][%%z]!
       echo/
       echo/
    )
    
    pause
    

    编辑:新版本满足可变数量零的新要求

    @echo off
    setlocal EnableDelayedExpansion
    
    rem ========Creating four 2x2 arrays using three 1s and one 0 to populate each
    for /l %%z in (0,1,3) do (
        set "digits=1110" & set "num=4"
        for /l %%y in (0,1,1) do (
            for /l %%x in (0,1,1) do (
                rem Get a random number between 0 and "num"
                set /A ran=!random!%%num, ranP1=ran+1, num-=1
                rem Use it to extract a random digit from "digits"
                for /F "tokens=1,2" %%i in ("!ran! !ranP1!") do (
                    set "map[%%x][%%y][%%z]=!digits:~%%i,1!"
                    set "digits=!digits:~0,%%i!!digits:~%%j!"
                )
            )
        )
    )
    
    rem ========Result
    for /l %%z in (0,1,3) do (
       echo !map[0][0][%%z]!!map[1][0][%%z]!
       echo !map[0][1][%%z]!!map[1][1][%%z]!
       echo/
       echo/
    )
    
    pause
    

    【讨论】:

    • 感谢您的回复,但如果超过一个零怎么办?
    • 在这种情况下,这是一个不同的问题 ;)查看我的编辑...
    • 所以?我的新代码解决了你的问题吗?如果是这样,那么您可以通过绿色复选标记将其选为最佳答案...
    【解决方案2】:
    @ECHO OFF
    setlocal EnableDelayedExpansion
    rem ========Creating four 2x2 zero arrays
    :restart
    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
            )
        )
    )
    :: loading array randomly with 1s
    FOR /L %%c IN (1,1,3) DO (
     SET /a x=!random! %% 2, y=!random! %% 2, z=!random! %% 4
     for /l %%z in (0,1,3) do (
      for /l %%y in (0,1,1) do (
       for /l %%x in (0,1,1) do (
        IF %%z==!z! IF %%y==!y! IF %%x==!x! (
         IF !map[%%x][%%y][%%z]! == 1 GOTO restart
         SET /a map[%%x][%%y][%%z]=1
        )
       )
      )
     )
    )
    :: display
    FOR /L %%z IN (0,1,3) DO (
     FOR /L %%y IN (0,1,1) DO ECHO !map[0][%%y][%%z]!!map[1][%%y][%%z]!
     ECHO.
    )
    GOTO :EOF
    

    中间块设置要插入的 1 的数量。然后选择三个随机数并将其分配给xyz,三个嵌套的for/L 命令只需将%%x..%%z 设置为x..z 的每个可能值组合,但在metaviariable 中为方便。当循环选择正确的组合时,地图上的那个点就会被测试。如果是1,请重新开始。如果不是,请将其设置为1

    显示例程似乎很明显。

    【讨论】:

    • 我试图做的是将三个 1 随机存储到每个 2x2 数组中,因此应该有 12 个 1,但您的代码似乎总共只生成三个 1,看起来需要编译时间长。
    【解决方案3】:

    Aacini 的代码稍微缩短了一点,(由于标题文本)

    @Echo Off
    SetLocal EnableDelayedExpansion
    
    Rem ========Creating four 2x2 one arrays
    For /L %%A In (0 1 3) Do For /L %%B In (0 1 1) Do For /L %%C In (0 1 1
    ) Do Set "m[%%C%%B%%A]=1"
    
    Rem ========Putting one 0 in an element randomly
    For /L %%A in (0 1 3
    ) Do Set/A B=!Random!%%2,C=!Random!%%2&Set "m[!B!!C!%%A]=0")
    
    Rem ========Result
    For /L %%A In (0 1 3
    ) Do Echo !m[00%%A]!!m[10%%A]!&Echo !m[01%%A]!!m[11%%A]!&Echo(&Echo(
    
    Pause
    

    请不要优先接受这个答案而不是他们的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多