菜鸟写了一段代码通过日期判断星座的。
感觉自己用的是本办法,求效率更好一些的循环,但是自己不知道怎么写。
看见那么多If 都蛋疼。。注:最后一个判断条件,还有点问题,目前还没想怎么判断。      private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            int a = dateTimePicker1.Value.Year;
            txtage.Text = (DateTime.Now.Year - a).ToString();
            string b = dateTimePicker1.Value.Month.ToString();
            string c = dateTimePicker1.Value.Day.ToString();
            if (c.Length < 2)                   //如果天数小于两位,在后面加0.
            {
                c = c + 0;
            }
            int d = int.Parse(b + c);         //将字符串b和c相加,得出整数型
            if (d >= 120 && d <= 218)  //开始判断日期对应的星座。
            {
                comboBox1.SelectedIndex = 0;
            }
            if (d >= 219 && d <= 320)
            {
                comboBox1.SelectedIndex = 1;
            }
            if (d >= 321 && d <= 419)
            {
                comboBox1.SelectedIndex = 2;
            }
            if (d >= 420 && d <= 520)
            {
                comboBox1.SelectedIndex = 3;
            }
            if (d >= 521 && d <= 621)
            {
                comboBox1.SelectedIndex = 4;
            }
            if (d >= 622 && d <= 722)
            {
                comboBox1.SelectedIndex = 5;
            }
            if (d >= 723 && d <= 822)
            {
                comboBox1.SelectedIndex = 6;
            }
            if (d >= 823 && d <= 922)
            {
                comboBox1.SelectedIndex = 7;
            }
            if (d >= 923 && d <= 1023)
            {
                comboBox1.SelectedIndex = 8;
            }
            if (d >= 1024 && d <= 1122)
            {
                comboBox1.SelectedIndex = 9;
            }
            if (d >= 1123 && d <= 1221)
            {
                comboBox1.SelectedIndex = 10;
            }
            if (d >= 1222 && d <= 119)
            {
                comboBox1.SelectedIndex = 11;
            }

解决方案 »

  1.   

    List<int> list = new List<int>(){120,219,321,420,521,622,723,823,923,1024,1123,1222};
    for(int i = 0; i < list.Count; ++i)
    {
    if(d >= list[i])
    {
    comboBox1.SelectedIndex = i;
    }
    }
      

  2.   

    如何化简见楼上
    最后一个if直接用else或者用
    if ( d <= 119 || d >= 1222) 
      

  3.   


    List<int> list = new List<int>(){120,218,320,419,520,621,722,822,922,1023,1122,1221};
    for(int i = 0; i < list.Count; ++i)
    {
        if(i==0)
        {
            //第一个
             if(d >= list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }    if(i<list.Count)
        {
            //不是最后一个,条件相同
            if(d > list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
        if(i==list.Count)
        {
            //最后一个,条件你自己写
            if(d >= list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
    }
      

  4.   


    List<int> list = new List<int>(){119,218,320,419,520,621,722,822,922,1023,1122,1221};
    for(int i = 0; i < list.Count; ++i)
    {
        if(i<list.Count)
        {
            //不是最后一个,条件相同
            if(d > list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
        if(i==list.Count)
        {
            //最后一个,条件你自己写
            if(d >= list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
    }
      

  5.   

    LZ是想实现 输入了生日以后 cbx里面自动选择相应的星座 对吧?
    如果是这样的话 我建议用LZ的代码要体现出一个清晰的思路
    不要太乱了了  不然以后代码多了 就算有注释 维护起来也很麻烦
    因为有可能是别人接手你的代码进行维护 代码乱的话 有注释也看不明白比如 我的思路是 既然要实现这样一个功能 我就需要拿选择的生日去星座的日期范围里做判断
    既然这样 就需要阵列 你可以用一个 也可以用两个 
    我习惯用一个二维数组 清晰明了
    比较的时候 拿生日去和二维数组的左右两个比 
    在哪个范围里  就让数组的index=cbx的index当然  用上面的也挺好 
      

  6.   

    List<int> list = new List<int>(){120,218,320,419,520,621,722,822,922,1023,1122,1221};
    for(int i = 0; i < list.Count; ++i)
    {
        if(i==0)
        {
            //第一个
             if(d >= list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }    if(i<list.Count)
        {
            //不是最后一个,条件相同
            if(d > list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
        if(i==list.Count)
        {
            
            if(d >= list[i]&&d <= list[i+1])
            {
                 comboBox1.SelectedIndex = i;
            }
        }
    }
      

  7.   

    总结了一下楼上几位前辈的意见,小小地整合了一下private void data_valueChanged(int d)
            {
                List<int> list = new List<int>() { 120, 219, 321, 420, 521, 622, 723, 823, 923, 1024, 1123, 1222 };
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    if (d >= list[i])
                    {
                        comboBox1.SelectedIndex = i;
                        return;
                    }
                }
                comboBox1.SelectedIndex = 11;
            }
      

  8.   

     if (d >= 1222 && d <= 119)
      {
      comboBox1.SelectedIndex = 11;
      }LZ你的最后一个if好像不成立哦...改成或吧。