本文介绍了Windows批处理上的For Loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些批处理练习,我正在尝试做一个循环以向后倒数,从110到100计数,但仅对偶数计数.我几乎让它工作了,但是由于某种原因,每次for循环运行时totalCount都不会更新.最后,它将总计打印为100,它只是循环的最后一个数字.我在做什么错了?

I am doing some batch practice and I am trying to do a loop to go backwards and count the numbers from 110 to 100, but only the even numbers. I almost got it to work, but for some reason totalCount is not updating every time the for loop goes around. At the end it prints the total as 100 which is simply the last number of the loop. What am I doing wrong?

::echo off
setlocal enableextensions
setlocal enabledelayedexpansion

set /a totalCount = 0

for /l %%x in (110, -2, 100) do (
   set /a totalCount = %totalCount% + %%x
)

echo total is %totalCount%

推荐答案

尝试将%totalCount%更改为!totalCount!.因此,代码应如下所示:

Try changing %totalCount% to !totalCount!. Therefore, the code should look like this:

echo off
setlocal enabledelayedexpansion

set /a totalCount = 0

for /l %%x in (110, -2, 100) do (
   set /a totalCount = !totalCount! + %%x
)

echo total is !totalCount!

这篇关于Windows批处理上的For Loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:40