Loops 周期增量

Loops 周期增量,loops,actionscript,increment,Loops,Actionscript,Increment,我在增加一个给定的循环极限值,比如说6。我很好奇我们怎样才能把代码缩短 function Cycle_Value(inc_value:Number = 5, times:Number = 3):Number { var return_value:Number = inc_value; while(times >= 1) { if(inc_value < 6) { inc_value++;

我在增加一个给定的循环极限值,比如说6。我很好奇我们怎样才能把代码缩短

function Cycle_Value(inc_value:Number = 5, times:Number = 3):Number
{
    var return_value:Number = inc_value;        

    while(times >= 1)
    {
       if(inc_value < 6)
       {
          inc_value++;
          return_value = inc_value;
       }
       else if(inc_value == 6)
       {
          return_value = 1;
          inc_value = 1;
       }
       else
          trace(inc_value+" is out of bounds");

        times--;
    }
    return return_value;
}

Sample Output: Cycle_Value(5,2) = 1, Cycle_Value(6,10) = 4
功能循环值(inc\u值:Number=5,次数:Number=3):Number
{
var返回值:数值=inc\u值;
而(次数>=1)
{
如果(inc_值<6)
{
inc_value++;
返回_值=inc_值;
}
否则如果(inc_值==6)
{
返回_值=1;
inc_值=1;
}
其他的
跟踪(inc_值+“超出范围”);
时代--;
}
返回_值;
}
样本输出:周期_值(5,2)=1,周期_值(6,10)=4
试试这个:

function Cycle_Value(inc_value:Number = 5, times:Number = 3):Number
{
   return (inc_value -1 + times) % 6 + 1; 
}
有关模运算符的更多信息,请访问:

注1:如果您愿意处理0范围内的数字。。5而不是1。。6,你可以简单地使用以下表达式

(inc_value + times) % 6

注2:就个人而言,我会使用不同的名称,例如value而不是inc_value,inc而不是times

这是动作脚本。穆罕默德现在删除了C++标签。MyTiADIS KoKoNIDISGR8公式,我已经整理出它的递减公式:返回(CurnValue+(Cyclix LimIT-1)-次)% Cyclix限制+1;