簡體   English   中英

批處理循環-變量問題

[英]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 ,因此需要callset ,因此需要將% s的常規數量加倍-與call echo相同。

我已經進行了重定向,以便結果僅顯示在屏幕上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM