Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# For循环索引在嵌套While循环后重置为0_C#_Loops_For Loop_While Loop - Fatal编程技术网

C# For循环索引在嵌套While循环后重置为0

C# For循环索引在嵌套While循环后重置为0,c#,loops,for-loop,while-loop,C#,Loops,For Loop,While Loop,作为一名编码执行者,我正在研究不同排序算法的可视化。我有一个按钮,可以暂停算法并以条形图的形式显示数组的每一项。为了实现这一点,我在排序的两个for循环中有一个嵌套的while循环。它循环通过,直到我再次按下按钮,一个布尔变量被设置为true,排序继续 但是,在程序退出while循环后,for循环的两个索引(i和j)被重置为0,排序再次从开始处开始 swap()和draw()都是自定义函数,它们的功能与名称的含义几乎完全相同 以下是我的排序代码: for (i = 0; i < items

作为一名编码执行者,我正在研究不同排序算法的可视化。我有一个按钮,可以暂停算法并以条形图的形式显示数组的每一项。为了实现这一点,我在排序的两个for循环中有一个嵌套的while循环。它循环通过,直到我再次按下按钮,一个布尔变量被设置为true,排序继续

但是,在程序退出while循环后,for循环的两个索引(i和j)被重置为0,排序再次从开始处开始

swap()和draw()都是自定义函数,它们的功能与名称的含义几乎完全相同

以下是我的排序代码:

for (i = 0; i < items.Count(); i++)
    {
        for (j = 0; j < items.Count() - 1 - i; j++)
        {
            //lbl_i.Text = Convert.ToString(i);
            //lbl_j.Text = Convert.ToString(j);

            if (items[j] > items[j + 1])
            {
                swap(j, j + 1); //swaps the items at two given indecies
                draw(); // draws the array to the picturebox
            }

            while (sorting == false) //the sorting is paused
            {
                Application.DoEvents();
            }
        }
    }
for(i=0;i项目[j+1])
{
交换(j,j+1);//交换两个给定索引的项
draw();//将数组绘制到picturebox
}
while(sorting==false)//排序暂停
{
Application.DoEvents();
}
}
}
你知道为什么会这样吗

我怀疑应用程序.DoEvents()-call可能有问题,但我需要它,以便按下按钮

另外,如果您注意到我的代码中还有其他我可以做得更好的地方,请告诉我,我在编码方面不是很有经验,因此欢迎您提供任何帮助和建设性的批评意见。:-)

谢谢大家!


Benjamin

如果不在for循环中创建i和j,i和j可能会在应用程序的其他地方发生更改。试试这个:

for (int i = 0; i < items.Count(); i++)
{
    for (int j = 0; j < items.Count() - 1 - i; j++)
    {
        //lbl_i.Text = Convert.ToString(i);
        //lbl_j.Text = Convert.ToString(j);

        if (items[j] > items[j + 1])
        {
            swap(j, j + 1); //swaps the items at two given indecies
            draw(); // draws the array to the picturebox
        }

        while (sorting == false) //the sorting is paused
        {
            Application.DoEvents();
        }
    }
}
for(int i=0;i项目[j+1])
{
交换(j,j+1);//交换两个给定索引的项
draw();//将数组绘制到picturebox
}
while(sorting==false)//排序暂停
{
Application.DoEvents();
}
}
}

变化在:int i,int j

如果不在for循环中创建i和j,i和j可能会在应用程序的其他地方发生变化。试试这个:

for (int i = 0; i < items.Count(); i++)
{
    for (int j = 0; j < items.Count() - 1 - i; j++)
    {
        //lbl_i.Text = Convert.ToString(i);
        //lbl_j.Text = Convert.ToString(j);

        if (items[j] > items[j + 1])
        {
            swap(j, j + 1); //swaps the items at two given indecies
            draw(); // draws the array to the picturebox
        }

        while (sorting == false) //the sorting is paused
        {
            Application.DoEvents();
        }
    }
}
for(int i=0;i项目[j+1])
{
交换(j,j+1);//交换两个给定索引的项
draw();//将数组绘制到picturebox
}
while(sorting==false)//排序暂停
{
Application.DoEvents();
}
}
}

改变是:inti,intj找到了答案:问题是,我最初是从同一个按钮调用这个函数的。在click事件中,我有一个if语句来检查按钮是说“暂停”还是“开始”,但当我暂停程序时,它又说“开始”。因此,当我单击它时,会再次调用该函数,并将int I和int j重新初始化为0

单击事件中的代码:

    private void btn_start_Click(object sender, EventArgs e)
    {
        //new added code
        if (btn_start.Text == "Start")
        {
            btn_start.Text = "Pause";
            sorting = true;
            sort();
        }
        else if (btn_start.Text == "Pause")
        {
            btn_start.Text = "Continue"; // '= "Start"' in the old code
            tmr_sorting.Stop();
            sorting = false;
        } 
        // new added code
        else if (btn_start.Text == "Continue")
        {
            btn_start.Text = "Pause";
            sorting = true;
        }
    }

找到答案:问题是,我最初是从同一个按钮调用这个函数的。在click事件中,我有一个if语句来检查按钮是说“暂停”还是“开始”,但当我暂停程序时,它又说“开始”。因此,当我单击它时,会再次调用该函数,并将int I和int j重新初始化为0

单击事件中的代码:

    private void btn_start_Click(object sender, EventArgs e)
    {
        //new added code
        if (btn_start.Text == "Start")
        {
            btn_start.Text = "Pause";
            sorting = true;
            sort();
        }
        else if (btn_start.Text == "Pause")
        {
            btn_start.Text = "Continue"; // '= "Start"' in the old code
            tmr_sorting.Stop();
            sorting = false;
        } 
        // new added code
        else if (btn_start.Text == "Continue")
        {
            btn_start.Text = "Pause";
            sorting = true;
        }
    }

使用调试并查看!你是从同一个按钮上的点击事件中调用这个函数的吗?我觉得你好像在打破这些for循环,重新开始。如果没有剩下的代码,很难说问题出在哪里;j