如何批量创建嵌套变量?

编程入门 行业动态 更新时间:2024-10-22 19:48:18
本文介绍了如何批量创建嵌套变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建的批处理游戏中获取嵌套变量. 我希望它可以选择一个随机变量并将其更改为X,但是如果已经选择它,则应返回并选择另一个数字.

I am trying to get nested variables in my batch game i am creating. I want it so that it will choose a random variable and change it to X, but if it is already chosen, it should go back and choose a different number.

set 1=a set 2=b set 3=c set 4=d set 5=e

这些是变量,这是代码

setlocal enabledelayedexpansion :eliminator set /a eliminate=(%random * 5) / 32767 + 1 if %%eliminate%%==X goto eliminator echo The letter !!eliminate!! was chosen timeout 5 set %%eliminate%%=X goto eliminator

现在,事情是,当我尝试回显它时,它会写出变量的名称而不是值.同样,已经选择的变量将再次被选择.有什么办法可以解决这个问题吗?谢谢.

Now, the thing is, when I try to echo it, it writes the name of the variable instead of the value. Also, variables that have already been chosen are being chosen again. Any way I could fix this? Thanks.

推荐答案

尝试一下:

@echo off&setlocal set "var1=a" set "var2=b" set "var3=c" set "var4=d" set "var5=e" :loop set /a rd=%random%%%5+1 if defined var%rd% call echo %%var%rd%%% set "var%rd%=" set "var" >nul 2>&1 && goto:loop

..输出(可能有所不同):

..output (may vary):

d a c b e

更多推荐

如何批量创建嵌套变量?

本文发布于:2023-10-26 18:59:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   变量   批量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!