为什么我的for循环不批量使用随机工作? [英] Why won't my for loop using random work in batch?

查看:119
本文介绍了为什么我的for循环不批量使用随机工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@echo off
goto :food
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)

:fruits
set i=0
for %%a in (apple banana grape lime) do (
   set /A i+=1
   set fruit[!i!]=%%a
)
set /a fruit=%random%%%4+1
set fruit=!fruit[%fruit%]!
exit /B

:food
for /l %%x in (1, 1, 5) do (
call :fruits
call :colorEcho 70 !fruit!
echo/
)
pause

exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

由于某些原因,我不知道,此代码无法正确输出.它只输出空格,因为出于某种原因,永远不会填充水果"变量.谁能向我解释一下?我还有另一个脚本,该脚本使用类似的结构也可以正常工作,但是从该脚本中提取这些部分会完全破坏它……任何帮助都将不胜感激!

For some reason that I don't know, this code does not output correctly. It outputs just blank spaces because for some reason the "fruit" variable is never being filled. Can anyone explain this to me? I have another script which works fine using a similar structure but extracting these parts out of that script totally breaks it...any help is much appreciated!

推荐答案

您在接受的答案中具有正确的代码在您之前的问题.因为您的代码不起作用的某些原因"是您在这种代码中引入的修改,因此您只需要复制相同的方案即可.

You have the right code in the accepted answer at your previous question. The "some reason" because your code does not work is the modifications you introduced in such a code, so you just need to duplicate the same scheme...

有多种方法可以实现同一件事.我总是喜欢最简单的一种.例如:

There are multiple ways to achieve the same thing. I always prefer the simplest one; for example:

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"

:food
for /l %%x in (1, 1, 5) do (
   call :fruits
   call :colorEcho 70 !fruit!
   echo/
)
pause
goto :EOF


:fruits
set /A i=%random%%%4+1
for /F "tokens=%i%" %%a in ("apple banana grape lime") do set fruit=%%a
exit /B

:colorEcho color text
set /P "=%BS% " > "%~2" <nul
findstr /A:%1 "^" "%~2" nul
del "%~2"
exit /B

这篇关于为什么我的for循环不批量使用随机工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆