[英]Batch for loop - Variable issue
我必须bash和批处理。 我陷入了批处理循环部分。 部分作业的说明如下:
::Doing a for loop from 1 to 100,
:: - Find the results of calculating each number from 1 to 100 mod 5.
:: - Append each number to the results.txt file
:: - After the for loop ends calculate and display the average of the results
:: - Append the average of the numbers to the results.txt file
::Code
:forLoop
echo.
::set /A temp-0
for /L %%x in (1, 1, 100) do (
set /A result="%%x %% 5"
call echo %%result%% >> results.txt
::%%temp+=%%result%%
)
::average=temp/100
::append average
GOTO exit
其他用户为我提供了结果变量和mod5。但是,我目前在使用temp时遇到了麻烦。 我认为,一旦我开始工作,就应该能够使普通零件正常工作而不会出现太多问题。 我的教授还提到批量处理了3种不同的for循环,因此我什至不确定我是否使用了正确的循环。 谁能帮我解决这个问题。
echo.
set /A temp=0
for /L %%x in (1, 1, 100) do (
set /A result="%%x %% 5"
call echo %%result%%
CALL SET /a temp+=%%result%%
)
SET /a average=temp/100
ECHO %average% %temp%
这很简单。 不要在块(带括号的语句系列)中使用::
注释样式,因为它实际上是破坏循环的破坏标签。
除此之外,由于不使用delayedexpansion
,因此需要call
该set
,因此需要将%
s的常规数量加倍-与call echo
相同。
我已经进行了重定向,以便结果仅显示在屏幕上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.