计算批处理中循环结果的平均值10 [英] calculating the average of 10 for loop results in batch

查看:110
本文介绍了计算批处理中循环结果的平均值10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个for循环,该循环为1-10(含)之间的每个数字计算(x mod 6)+ 2,其中"x"代表循环递增时每个单独的数字.但是,我想计算10个结果中每个结果的平均值,但实际上我不知道如何格式化循环.我宁愿不做类似echo "1 + 2 + 3 + etc"的事情,因为这似乎非常懒惰且结构不良.我觉得这是相对容易的事情,但是我尝试搜索它并尝试另一个循环,但无济于事.任何帮助都将不胜感激!

so i've created a for loop that calculates (x mod 6) + 2 for every number from 1-10 (inclusive), with "x" representing each individual number as the loop increments. however, i'd like to calculate the average for each of the 10 results, but i actually have no idea as to how i'd format the loop to do so. i'd rather not do something like echo "1 + 2 + 3 + etc" as that seems extremely lazy and ill-constructed. i feel like this is something relatively easy, but i've tried searching it up and attempting another loop, but to no avail. any and all help is much appreciated!

这是循环:

:ForLoop
@echo off &setlocal enabledelayedexpansion

echo This loop calculates the results for (x mod 6) + 2 with x being each number from1-10 (inclusive).
echo.
for /L %%x in (1, 1, 10) do (
    SET /a VAR=%%x %% 6+2
    set /a CAL=!VAR! %% 8
    echo %%x MOD 6 + 2 = !CAL!
)

推荐答案

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
:ForLoop

echo This loop calculates the results for (x mod 6) + 2 with x being each number from1-10 (inclusive).
echo.
SET /a iterations=10
SET /a sum=0
for /L %%x in (1, 1, %iterations%) do (
    SET /a VAR=%%x %% 6+2
    SET /a sum+=var
    SET /a avg=sum / %%x
    echo %%x MOD 6 + 2 = !var! average so far=!avg! (from !sum! / %%x^)
)

GOTO :EOF

不需要做%% 8爵士乐-因为var只能是(0..5)+2,所以范围是2..7.

There's no need to do the %% 8 jazz - since var can only be (0..5)+2, this gives a range of 2..7.

set /a使用变量的运行时值,因此不需要!var!-%var%将使用var initial 值.

set /a uses the run-time value of the variables, so !var! is not required - %var% would use the initial value of var.

请注意,平均数与整数数学的批处理一样被截断.

Note that the average is truncated as batch does integer mathematics.

这篇关于计算批处理中循环结果的平均值10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆