NET|星座问题

2017-11-20  本文已影响0人  皮卡球ca
timg.jpg

task中有个需求,需要根据身份证号获取用户的星座,我首先想到根据日期匹配日期区间,代码写起来不太顺利。查资料后,感觉这个思路不错,将日期的比对转换为浮点数比对。

 public static string GetAtomFromBirthday(DateTime birthday, ref string Constellationtxt)
    {
        float birthdayF = 0.00F;

        if (birthday.Month == 1 && birthday.Day < 20)
        {
            birthdayF = float.Parse(string.Format("13.{0}", birthday.Day));
        }
        else
        {
            birthdayF = float.Parse(string.Format("{0}.{1}", birthday.Month, (birthday.Day > 9 ? birthday.Day.ToString() : "0" + birthday.Day)));
        }
        float[] atomBound = { 1.20F, 2.20F, 3.21F, 4.21F, 5.21F, 6.22F, 7.23F, 8.23F, 9.23F, 10.23F, 11.21F, 12.22F, 13.20F };
        string[] atoms = { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
        string[] atomscode = { "11 (阳历:1.20~2.18)", "12 (阳历:2.19~3.20)", "01 (阳历:3.21~4.19)",
                                 "02 (阳历:4.20~5.20)", "03 (阳历:5.21~6.21)", "04 (阳历:6.22~7.22)",
                                 "05 (阳历:7.23~8.22)", "06 (阳历:8.23~9.22)", "07(阳历:9.23~10.23)", 
                                 "08 (阳历:10.24~11.22)", "09 (阳历:11.23~12.21)", "10 (阳历:12.22~1.19)" };

        string ret = string.Empty;
        for (int i = 0; i < atomBound.Length - 1; i++)
        {
            if (atomBound[i] <= birthdayF && atomBound[i + 1] > birthdayF)
            {
                ret = atomscode[i];
                Constellationtxt = atoms[i];
                break;
            }
        }
        return ret;
    } 

但有个坑,需要注意日期号码为个位数时,应在前面加0.(我已因此被喷过了。)

上一篇下一篇

猜你喜欢

热点阅读