Switch-Case块没有按预期执行?

时间:2014-09-29 04:54:04

标签: c# switch-statement

在下面的代码片段中,我打算在每个计时器刻度事件中,它应该落在下一个' switch语句的情况。 然而,正如我跑,它首先经历奇数案件然后甚至案件?

我犯了什么错误?

  private void timer1_Tick(object sender, EventArgs e)
  {
        switch (SCROLL_SCREEN_NO)
        {
            case 0: 
                    SCROLL_SCREEN_NO = 1;
                    break;

            case 1: label1.Text = "Param1";
                    label2.Text = "1234";
                    SCROLL_SCREEN_NO = 2;
                    break;

            case 2: label1.Text = "Param2";
                    label2.Text = "5678";
                    SCROLL_SCREEN_NO = 3;
                    break;

            case 3: label1.Text = "Param3";
                    label2.Text = "9012";
                    SCROLL_SCREEN_NO = 0;
                    break;

            case 4: label1.Text = "Param4";
                    label2.Text = "0";
                    SCROLL_SCREEN_NO = 5;
                    break;

            case 5: label1.Text = "Param5";
                    label2.Text = "02";
                    SCROLL_SCREEN_NO = 0;
                    break;

            default: { break; }
        }
    }

2 个答案:

答案 0 :(得分:2)

你的代码真的是错误的,试试这个:

private void timer1_Tick(object sender, EventArgs e)
{
   string[] L1Keys = new string[]{"Param1", "Param2","Param3","Param4","Param5"};
   string[] L2Keys = new string[]{"1234", "5678","9012","0","02"};
   label1.Text = L1Keys[SCROLL_SCREEN_NO];
   label2.Text = L2Keys[SCROLL_SCREEN_NO];

   if(SCROLL_SCREEN_NO >=0 && SCROLL_SCREEN_NO<=4)
       SCROLL_SCREEN_NO = (SCROLL_SCREEN_NO+1) % 5;
}

答案 1 :(得分:0)

您的代码似乎没问题。 您是否在此计时器刻度事件之外使用SCROLL_SCREEN_NO? 如果是,那么请再次查看使用SCROLL_SCREEN_NO的代码。