英文:
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;
<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>
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论