每循环获取第100个值 [英] Get every 100th value in a loop

查看:85
本文介绍了每循环获取第100个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使这个更清洁,而不是像我这里做的那样使用tempvalue?
$ b




更新代码有一个逻辑错误,并没有显示我在做什么。
这就是我所做的:

  var loopTempValue = noOfPackets / 100; 
for(i = 0; i< noOfPackets; i ++)
{
\\DoStuff

if(i == loopTempValue)
{
loopTempValue = loopTempValue +(noOfPackets / 100);
UploadBackGroundWorker.ReportProgress(pross);






UPDATE最终

这是如何修复后的反馈,thx的家伙。 $ c> if(i%(noOfPackets / 100)== 0&& i!= 0)
{
UploadBackGroundWorker.ReportProgress(pross);


解决方案

  if(i%100 == 0& i!= 0){//您的代码} 

Modulus对于这样的检查来说太棒了。

更多关于Modulus的信息 - http:// www.vias.org/cppcourse/chap04_01.html



更新:我加了&&如果你想使用tempvalue而不是硬编码100,那么这将是一个解决方案:

pre $ if(i%tempvalue == 0&& i!= 0){// YOUR CODE}


Is there a way to make this cleaner and not use the tempvalue like i have done here?


UPDATE the code had a logic bug and didn't show what I'm doing. This is what I'm doing:

var loopTempValue = noOfPackets / 100;
for(i=0; i < noOfPackets; i++)
{   
    \\DoStuff

    if (i == loopTempValue)
    {
         loopTempValue = loopTempValue + (noOfPackets / 100);
         UploadBackGroundWorker.ReportProgress(pross);
    }
}


UPDATE Final

This is how its fixed after the feedback, thx guys.

if (i % (noOfPackets / 100) == 0 && i != 0)
{
     UploadBackGroundWorker.ReportProgress(pross);
}

解决方案

if (i % 100 == 0 && i != 0) { //YOUR CODE }

Modulus is fantastic for checks like this.

More on Modulus - http://www.vias.org/cppcourse/chap04_01.html

UPDATE: I added && i != 0 for the 0 case being true.

If you want to use the tempvalue instead of hard coding 100, then this would be a solution:

if (i % tempvalue == 0 && i != 0) { //YOUR CODE }

这篇关于每循环获取第100个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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