每循环获取第100个值 [英] Get every 100th value in a loop
问题描述
有没有一种方法可以使这个更清洁,而不是像我这里做的那样使用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
更新:我加了 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:
UPDATE Final This is how its fixed after the feedback, thx guys.
Modulus is fantastic for checks like this. More on Modulus - http://www.vias.org/cppcourse/chap04_01.html UPDATE: I added If you want to use the tempvalue instead of hard coding 100, then this would be a solution:
这篇关于每循环获取第100个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!&&如果你想使用tempvalue而不是硬编码100,那么这将是一个解决方案:
pre $ if(i%tempvalue == 0&& i!= 0){// YOUR CODE}
var loopTempValue = noOfPackets / 100;
for(i=0; i < noOfPackets; i++)
{
\\DoStuff
if (i == loopTempValue)
{
loopTempValue = loopTempValue + (noOfPackets / 100);
UploadBackGroundWorker.ReportProgress(pross);
}
}
if (i % (noOfPackets / 100) == 0 && i != 0)
{
UploadBackGroundWorker.ReportProgress(pross);
}
if (i % 100 == 0 && i != 0) { //YOUR CODE }
&& i != 0
for the 0 case being true.if (i % tempvalue == 0 && i != 0) { //YOUR CODE }