变量在多个循环内不递增

本文关键字:循环 变量 | 更新日期: 2023-09-27 18:00:05

这个问题让我很困惑。我有一个循环中共享的变量,它不是递增的。

有问题的变量是elrObject.currentLocation。有两种方法可以递增它——如果XML元素为空,或者不为空。

这是代码:

if (reader.Name == "Cell")
{  
    if (reader.IsEmptyElement)
    {       
        Response.Write("i ran<br>");
        elrObject.currentLocation++;
    }       
    else
    {
        while (reader.Read())
        {           
            if (reader.IsStartElement())
            {                                           
                //labels
                if (elrObject.currentLocation >= elrObject.index && elrObject.currentLocation <= elrObject.index + elrObject.colSpan)
                    Response.Write("i ran again<br>");
                Response.Write(elrObject.currentLocation + "<br>");
                elrObject.currentLocation++;
            }
            if (reader.Name == "Cell")
            break;
        }
    }
}

我得到的输出是:
0
1
2
3
5

数字4表示XML元素为空并且运行顶部循环的情况。我正在增加变量,但它不会显示数字4,它会完全跳过它,转到5。我确信上循环在下循环之前运行正常,因为以下循环也在运行:

我跑了
我再次跑步

这确认了上循环在下循环之前运行。。。。然而数字4却在跳过自己!我真的很感谢你的帮助。

变量在多个循环内不递增

不应该显示数字4。你没有

Response.Write(elrObject.currentLocation + "<br>");

在中

if (reader.IsEmptyElement)

块。