在变量中使用数字/变量显示不正确的问题

时间:2019-02-12 22:24:09

标签: batch-file cmd

昨天,我决定在命令提示符下玩一些乐趣:我想用unicode随机生成一个“迷宫”,并为此感到非常兴奋。我已经完成了很多工作,但是我的坐标系在使用字符制作图像时遇到了一些问题。由于某种原因,我过多的变量没有显示它们的值。我不知道怎么了可能是我创建变量时的语法(变量名中有变量),这可能是我不知道时设置它们的方式。

@echo off
:restart
set x=0
set y=0
:next
set /a chance=%random% %%2
if chance==1 (
    set %x%l%y%=00
)
if chance==2 (
    set %x%l%y%=11
)
set /a x=%x%+1
if x GEQ 10 (
    set x=0
    goto display
    set /a y=%y%+1
    if y GEQ 11 (
        goto display
    )
)
goto next
:display
echo %0l0%%1l0%%2l0%%3l0%%4l0%%5l0%%6l0%%7l0%%8l0%%9l0%
echo %0l1%%1l1%%2l1%%3l1%%4l1%%5l1%%6l1%%7l1%%8l1%%9l1%
echo %0l2%%1l2%%2l2%%3l2%%4l2%%5l2%%6l2%%7l2%%8l2%%9l2%
echo %0l3%%1l3%%2l3%%3l3%%4l3%%5l3%%6l3%%7l3%%8l3%%9l3%
echo %0l4%%1l4%%2l4%%3l4%%4l4%%5l4%%6l4%%7l4%%8l4%%9l4%
echo %0l5%%1l5%%2l5%%3l5%%4l5%%5l5%%6l5%%7l5%%8l5%%9l5%
echo %0l6%%1l6%%2l6%%3l6%%4l6%%5l6%%6l6%%7l6%%8l6%%9l6%
echo %0l7%%1l7%%2l7%%3l7%%4l7%%5l7%%6l7%%7l7%%8l7%%9l7%
echo %0l8%%1l8%%2l8%%3l8%%4l8%%5l8%%6l8%%7l8%%8l8%%9l8%
echo %0l9%%1l9%%2l9%%3l9%%4l9%%5l9%%6l9%%7l9%%8l9%%9l9%
echo:
pause
cls
goto restart

因为我还没有用Unicode替换“ 00”和“ 11”,所以所需的输出看起来像这样:

00110000111100111111
11110011110000111100
00000011111111001100
11000000111100110011
00110000111100111111
00110000111100111111
11110011110000111100
00000011111111001100
11000000111100110011
00110000111100111111

Press any key to continue...

相反,这是一团糟。

"C:\Users\***\Documents\array.bat"l0%1l0%2l0%3l0%4l0%5l0%6l0%7l0%8l0%9l0
"C:\Users\***\Documents\array.bat"l1%1l1%2l1%3l1%4l1%5l1%6l1%7l1%8l1%9l1
"C:\Users\***\Documents\array.bat"l2%1l2%2l2%3l2%4l2%5l2%6l2%7l2%8l2%9l2
"C:\Users\***\Documents\array.bat"l3%1l3%2l3%3l3%4l3%5l3%6l3%7l3%8l3%9l3
"C:\Users\***\Documents\array.bat"l4%1l4%2l4%3l4%4l4%5l4%6l4%7l4%8l4%9l4
"C:\Users\***\Documents\array.bat"l5%1l5%2l5%3l5%4l5%5l5%6l5%7l5%8l5%9l5
"C:\Users\***\Documents\array.bat"l6%1l6%2l6%3l6%4l6%5l6%6l6%7l6%8l6%9l6
"C:\Users\***\Documents\array.bat"l7%1l7%2l7%3l7%4l7%5l7%6l7%7l7%8l7%9l7
"C:\Users\***\Documents\array.bat"l8%1l8%2l8%3l8%4l8%5l8%6l8%7l8%8l8%9l8
"C:\Users\***\Documents\array.bat"l9%1l9%2l9%3l9%4l9%5l9%6l9%7l9%8l9%9l9

Press any key to continue . . .

我计划对此概念进行扩展,但是如果不解决这个问题,我将无所作为。请帮忙!

1 个答案:

答案 0 :(得分:1)

我强烈建议您在使用数组时使用标准数组符号,也就是说,将索引括在方括号中。您可以在Arrays, linked lists and other data structures in cmd.exe (batch) script

阅读更多详细信息

我建议您使用for /L命令来组装循环。

例如:

@echo off
setlocal EnableDelayedExpansion

:restart
for /L %%x in (0,1,9) do (
   for /L %%y in (0,1,9) do (
      set /A chance=!random! %% 2
      if !chance! == 1 (
         set "p[%%x][%%y]=00"
      ) else (
         set "p[%%x][%%y]=11"
      )
   )
)

:display
for /L %%x in (0,1,9) do (
   set "out="
   for /L %%y in (0,1,9) do (
      set "out=!out!!p[%%x][%%y]!
   )
   echo !out!
)
echo/
pause
cls
goto restart