无法循环播放此bat文件

时间:2018-09-12 16:15:17

标签: loops batch-file

for /l %i% in (1, 1, 8) do (
    set /p x=
    set /p y=
    @set  /a "z=%x%/%y%
    echo %z%
    pause
)

这只是一个问题的示例,我遇到了循环无法正常工作的情况。而且我不知道为什么。我看到命令提示符弹出仅打开了一秒钟。不应该那样做。

我希望重复此操作,而不必像这样多次复制和粘贴它:

set /p x=
set /p y=
@set  /a "z=%x%/%y%
echo %z%
pause
set /p x=
set /p y=
@set  /a "z=%x%/%y%
echo %z%
pause
set /p x=
set /p y=
@set  /a "z=%x%/%y%
echo %z%
pause
set /p x=
set /p y=
@set  /a "z=%x%/%y%
echo %z%
pause

这是我可以使用它的唯一方法。

1 个答案:

答案 0 :(得分:0)

基于您对For /L循环的错误使用,该循环应读为%%i,而不是%i%,并按照注释中提供的建议进行操作,脚本应类似于以下内容之一例子。

  1. 没有延迟的扩展:

    @Echo Off
    For /L %%A In (1,1,8) Do (
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Call Echo Quotient as integer: %%z%%
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Call Echo Quotient as integer: %%z%%
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Call Echo Quotient as integer: %%z%%
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Call Echo Quotient as integer: %%z%%
        Pause
    )
    
  2. 具有延迟的扩展:

    @Echo Off
    SetLocal EnableDelayedExpansion
    For /L %%A In (1,1,8) Do (
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Echo Quotient as integer: !z!
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Echo Quotient as integer: !z!
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Echo Quotient as integer: !z!
        Pause
        Set /P "x=Dividend: "
        Set /P "y=Divisor: "
        Set /A z=x/y
        Echo Quotient as integer: !z!
        Pause
    )
    

请在命令提示符下阅读两个主要命令的用法信息:

For /?

Set /?