輸入生日,然後顯示星座出來
一、簡述
撰寫程式,功能為輸入生日,然後顯示星座出來。
二、方法
在網路上查了一下,發現有一個不錯的範例 [星座查询: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;
}
執行結果
範例下載
三、相關連結與參考