.NET菜鳥自救會

小歐說 : 努力工作,用心學習
文章數 - 658, 回應數 - 770, 引用數 - 0

請大家幫忙投票,感恩yes

第五屆部落客百傑
第五屆部落客百傑

關於小歐ou

每月文章

文章分類

Office 365

常用文章

常用連結

微軟論壇

技術部落格

[C#]輸入生日算星座

 

一、簡述

撰寫程式,功能為輸入生日,然後顯示星座出來。

 

二、方法

在網路上查了一下,發現有一個不錯的範例 [星座查询:C#版根据生日查星座],依據它做改寫,程式碼如下所示
 

 

        private void brnProcess_Click(object sender, EventArgs e)
        {
            this.txtResult.Text = GetAtomFromBirthday(this.dtpBirthDay.Value);
        }

        /// <summary>
        /// 輸入生日取得星座
        /// </summary>
        /// <param name="birthday">生日</param>
        /// <returns>星座</returns>
        public string GetAtomFromBirthday(DateTime dtBirthDay)
        {
            float fBirthDay = Convert.ToSingle(dtBirthDay.ToString("M.dd"));
            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 ret = string.Empty;
            for (int i = 0; i < atomBound.Length - 1; i++)
            {
                if (atomBound[i] <= fBirthDay && atomBound[i + 1] > fBirthDay)
                {
                    ret = atoms[i];
                    break;
                }
            }
            return ret;
        }

 

執行結果

image  

範例下載

[C#]輸入生日算星座.zip

 

三、相關連結與參考

星座查询:C#版根据生日查星座

 


以下為小歐ou的簽名檔

  • 歡迎部分轉貼本站的文章,請記得在文章中附上超連結與站名【.NET菜鳥自救會】。
  • 有問題想發問請到【MSDN】、【TechNet】、【Answers】論壇。




posted on 2009/2/4 11:59 我要推薦 | 閱讀數 : 7131 | 文章分類 [ Develop-C# Develop-Desktop ] 訂閱

Feedback

# re: [C#]輸入生日算星座 回覆

我對星座沒研究, 各星座的日期我是參考
http://www.happy543.com.tw/star/date.html
用您的code,我小改了一下,您參考看看

static string GetAtomFromBirthday(DateTime birthday) {
float[] atomBound = { 1.21F, 2.20F, 3.21F, 4.20F, 5.21F, 6.22F, 7.23F, 8.23F, 9.23F, 10.24F, 11.22F, 12.21F };
string[] atoms = { "魔羯座", "水瓶座", "雙魚座", "牡羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蠍座", "射手座" };
float value = Convert.ToSingle(birthday.ToString("M.dd"));
for (int i = 0; i < atomBound.Length; i++) {
if (value < atomBound[i])
return atoms[i];
}

return "魔羯座";
}
2009/2/4 下午 01:40 | Allen Kuo

# re: [C#]輸入生日算星座 回覆

To Allen Kuo大大

程式精簡好多,而且我忘記可以用

Convert.ToSingle(birthday.ToString("M.dd"));
多謝分享啦

 

 

2009/2/4 下午 05:31 | chou

回應

標題
姓名
電子郵件 (將不會被顯示)
個人網頁
內容 
  登入後使用進階評論  
Please add 2 and 2 and type the answer here:

Powered by: