陷入无限循环C#

时间:2013-12-01 23:44:17

标签: c# loops while-loop

我有一个问题,当我想滚动程序冻结并且必须关闭它。我认为问题是系统陷入无限循环并崩溃(因为我认为内存耗尽)。以下是while的代码:

string acorde;
int ScrollPosition = this.vScrollBar1.Value;
if (ScrollPosition < 5)
{
    acorde = "A";
    lblnota.Text = "A/La";
    lblnota2.Text = "A/La";
    imagen.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\A.jpg");

    while(note != 1)
    {
        lblnote.Text = "A";
        lblcuerda.Text = "5° cuerda";
        lbltraste.Text = "";
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
    }
    imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");
    while(note != 8)
    {
        lblnote.Text = "E";
        lblcuerda.Text = "4° cuerda";
        lbltraste.Text = "2° traste";
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
    }
    imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");
    while(note != 1)
    {
        lblnote.Text = "A";
        lblcuerda.Text = "3° cuerda";
        lbltraste.Text = "2° traste";
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
    }
    imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");
    while (note != 5)
    {
        lblnote.Text = "C#";
        lblcuerda.Text = "2° cuerda";
        lbltraste.Text = "2° traste";
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
    }
    imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");

    while (note != 8)
    {
        lblnote.Text = "E";
        lblcuerda.Text = "1° cuerda";
        lbltraste.Text = "";
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
    }
    imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");


}
else if (ScrollPosition < 12)
{
    lblnota.Text = "Am/Lam";
    lblnota2.Text = "Am/Lam";
    imagen.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\A#.jpg");

我现在真的需要你的帮助。谢谢。

2 个答案:

答案 0 :(得分:3)

问题是您使用while循环。

while(note != 1)

这些循环的主体中没有任何东西可以改变这种状况。所以每次循环“好吧......注意不等于1?”......条件总是如此。所以循环再次执行。

答案 1 :(得分:2)

当然,你陷入无限循环。

您正在检查note != 1或其他比较,但我在note个循环中未看到while被更改。

一旦你进入其中一个循环,你将永远不会退出,因为note将始终具有相同的值。

以下是您的代码应该是什么样的。

if (ScrollPosition < 5)
        {
            acorde = "A";
            lblnota.Text = "A/La";
            lblnota2.Text = "A/La";
            imagen.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\A.jpg");

        if(note != 1)
        {
            lblnote.Text = "A";
            lblcuerda.Text = "5° cuerda";
            lbltraste.Text = "";
            imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
        }
        else if(note != 8)
        {
            lblnote.Text = "E";
            lblcuerda.Text = "4° cuerda";
            lbltraste.Text = "2° traste";
            imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
        }
        else if(note != 1)
        {
            lblnote.Text = "A";
            lblcuerda.Text = "3° cuerda";
            lbltraste.Text = "2° traste";
            imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
        }
        else if(note != 5)
        {
            lblnote.Text = "C#";
            lblcuerda.Text = "2° cuerda";
            lbltraste.Text = "2° traste";
            imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
        }
        else if(note != 8)
        {
            lblnote.Text = "E";
            lblcuerda.Text = "1° cuerda";
            lbltraste.Text = "";
            imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\MAL.jpg");
        }
        imagenok.Image = Image.FromFile(@"C:\Users\Sebastian\Desktop\GuitarraFINAL\Guitarist\Imagenes\OK.jpg");
    }