变量疑难解答

时间:2018-09-16 14:29:30

标签: batch-file cmd

set goto=loop应该会发生变化,因此当它回显它时,它应该输出“ loop”,但仍然输出“ start”。我似乎无法弄清楚这一点。

set cash=100
set goto=start

if %cash% GEQ 100 (
    set /A cash=%cash%-100
    set %goto%=DEAD
    set goto=loop
    echo %goto%
    goto %goto%
) else (
    goto %goto%
)

1 个答案:

答案 0 :(得分:0)

您的变量在代码块中正在更改,因此您应该使用该变量的延迟扩展:

要么:

@Echo Off
SetLocal EnableDelayedExpansion

Set "cash=100"
Set "goto=start"

If %cash% GEq 100 (
    Set /A cash=cash-100
    Set "%goto%=DEAD"
    Set "goto=loop"
    Echo !goto!
    GoTo !goto!
) Else (
    GoTo %goto%
)

或者:

@Echo Off
SetLocal EnableDelayedExpansion

Set "cash=100"
Set "goto=start"

If %cash% GEq 100 (
    Set /A cash -=100
    Set "%goto%=DEAD"
    Set "goto=loop"
    Echo !goto!
    GoTo !goto!
) Else (
    GoTo %goto%
)

与您之前的问题一样,请查看Set命令(即Set /?

)的用法信息