如何在循环中重置计数器 [英] How to reset counter in loop
本文介绍了如何在循环中重置计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
int i = 0;
while (i < 10)
{
i++;
i = i % 10;
}
以上代码在达到极限时将计数器重置为0.只是想在下面的代码中有没有办法做类似的事情来代替if条件
The above code resets the counter to 0 when it reaches the limit. Just wanted to Is there a way to do some thing similar in the below code replacing the if condition
int i = 10;
while (i > -1)
{
i--;
if (i == -1)
i = 10;
}
推荐答案
两个循环都是 while(true)
,因此它们在问题中没有任何作用.
Both your loops are while(true)
so they play no role in the question.
这留下了:我可以在没有if的情况下倒数倒计时吗?"
That leaves: "can I make a count-down roll over without an if?"
如果您确实想要:答案是肯定的.
The answer is yes, if you realy want to:
i--;
i = (i + 10) % 10;
这篇关于如何在循环中重置计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文