如何使外部goto不会批量中断for循环 [英] How to make an external goto not break a for loop in batch

查看:210
本文介绍了如何使外部goto不会批量中断for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个脚本作为宠物项目写的,看起来似乎比原来要简单...我有一个for循环,用于根据用户输入增加一定次数,但是在在for循环中,我在脚本的前面有一个goto的目标语句.问题是,当使用该goto时,它将中断for并导致for循环仅提供1个答案.

So I have a script that I'm writing as a pet project that seemed simpler than it turns out to be...I have a for loop that is meant to increment a certain amount of times based on user input but in the for loop, I have a destination statement for a goto earlier in the script. The issue is that when that goto is used it breaks the for and causes the for loop to only provide 1 answer.

这是代码:

for /l %%x in (1, 1, %player%) do (
:defense
call :defenders
:defense_return
call :colorEcho 0e %operator%
echo.
)

:defense_return是罪魁祸首,但这是必需的,因为我正在使用%RANDOM%,因此我需要它参考使用%RANDOM%的时间,否则,当我确实想要2个不同的输出时,我只会得到相同的输出

The :defense_return is the culprit but it's neccesary because I'm using %RANDOM% so I need it to reference back to when the %RANDOM% is used or else I just get the same output when I really want 2 different outputs.

这是防御者区:

:defenders
set /a operator=%random%%%18+1
REM 707th SMB
if %operator%== 1 set operator=Vigil
REM G.R.O.M
if %operator%== 2 set operator=Ela
REM S.D.U
if %operator%== 3 set operator=Lesion
REM G.E.O
if %operator%== 4 set operator=Mira
REM S.A.T
if %operator%== 5 set operator=Echo
REM BOPE
if %operator%== 6 set operator=Caviera
REM Navy Seal
if %operator%== 7 set operator=Valkyrie
REM JTF2
if %operator%== 8 set operator=Frost
REM S.A.S
if %operator%== 9 set operator=Mute
if %operator%== 10 set operator=Smoke
REM SWAT
if %operator%== 11 set operator=Castle
if %operator%== 12 set operator=Pulse
REM GIGN
if %operator%== 13 set operator=Doc
if %operator%== 14 set operator=Rook
REM GSG9
if %operator%== 15 set operator=Jager
if %operator%== 16 set operator=Bandit
REM Spetsnaz
if %operator%== 17 set operator=Tachanka
if %operator%== 18 set operator=Kapkan
goto :defense_return

我真的很想让这个脚本生效,但是这个for循环导致我遇到问题...任何帮助都非常感激!

I really want to make this script work out, but this for loop is causing me issues...Any help is much appreciated!

推荐答案

您在这里有一个误解,如评论中所解释的...

You have a misconception here, as already explained in the comments...

:defenders块是一个子程序,即您要调用(或 invoke 执行,或执行任意操作,当这样的代码段结束时,您希望引用回"(转到)另一个位置,而是返回调用该子程序的位置.返回到call命令后面的命令的方式是exit /B,尽管goto :EOF也可以用于此目的,但是它更加混乱(在大多数编程语言中, statement 称为return).

The :defenders block is a subprogram, that is, a code segment that you want to call (or invoke, or execute, or whichever) and when such a code segment ends you want not to "reference back" (goto) another place, but to return to the point where such a subprogram was called. The way to return to the command that follows the call command is exit /B, although goto :EOF also works for this purpose, but it is more confusing (in most programming languages, the statement to do this is called return).

此外,当您想使用在for循环内更改 的变量的值时,必须使用!variable!构造而不是%variable%构造(并插入程序开头的命令.

Also, when you want to use the value of a variabe that change inside a for loop, you must use the !variable! construct instead of %variable% one (and insert setlocal EnableDelayedExpansion command at beginning of the program).

最后,如果使用数组,您的代码可能会得到改善.这是包含所有先前描述的修改之后的代码的最终版本:

Finally, your code may be improved if you use an array. This is the final version of your code after including all previously described modifications:

@echo off
setlocal EnableDelayedExpansion

rem Initialize "operator" array
set i=0
for %%a in (Vigil Ela Lesion Mira Echo Caviera Valkyrie Frost Mute
            Smoke Castle Pulse Doc Rook Jager Bandit Tachanka Kapkan) do (
   set /A i+=1
   set operator[!i!]=%%a
)

rem Get a Back-Space (ASCII 8) character (for :colorEcho routine)
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"

set player=4

for /l %%x in (1, 1, %player%) do (
call :defenders
call :colorEcho 0e !operator!
echo/
)
pause
goto :EOF

:defenders
set /a operator=%random%%%18+1
set operator=!operator[%operator%]!
exit /B

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

这篇关于如何使外部goto不会批量中断for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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