无法在for循环内增加计数器。

huangapple go评论63阅读模式
英文:

Cannot increase the counter inside a for loop

问题

<div class="forecast-list">
    @for (int i=1; i<13;i++)
    {
         <div style="position:relative;left:@onScreenPosition;">@CalcRevenuesChange(Incomes[i].Quantity,Forecast[i].Quantity) </div>
            onScreenPosition += 2;

    }
</div>

计数器仍然保持为0,我尝试在@CalcRevenuesChange()内增加它,但仍然不起作用。我期望的输出是:
0 0 0 0 0 0 0 0 0 0 0 0 0 0

但它仍然保持为0000000000000
........................................

英文:
<div class="forecast-list">
    @for (int i=1; i<13;i++)
    {
         <div style="position:relative;left:@onScreenPosition;">@CalcRevenuesChange(Incomes[i].Quantity,Forecast[i].Quantity) </div>
            onScreenPosition += 2;

    }
</div>

the counter still remains 0, I tried to increase him inside @CalcRevenuesChange() but still doesn’t work. The output that I expected for is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0

but it's still remains 0000000000000
........................................

答案1

得分: 0

在元素内创建的变量将始终被重置。尝试这样做。

@{
    int count = 1;
    <div class="forecast-list">
        @for (; count < 13; count++)
        {
            <div style="position:relative;left:@onScreenPosition;">
                @CalcRevenuesChange(Incomes[count].Quantity, Forecast[count].Quantity)
            </div>
            onScreenPosition += 2;
        }
    </div>
}
英文:

Variables created inside element will always be reset. Try this.

 @{
        int count=1;
        &lt;div class=&quot;forecast-list&quot;&gt;
            @for (; count&lt;13;count++)
            {
                 &lt;div style=&quot;position:relative;left:@onScreenPosition;&quot;&gt;@CalcRevenuesChange(Incomes[count].Quantity,Forecast[count].Quantity) &lt;/div&gt;
                    onScreenPosition += 2;
        
            }
        &lt;/div&gt;
}

huangapple
  • 本文由 发表于 2023年6月1日 15:18:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定