問題的提出:
在現代社會中,一個人總是離不開數字。人在社會上總要有一個身份證號碼,學生在學校里讀書一定會有一個學號,而且這些號碼不都是一些無意義的數字。我寫的這個程序是用來分析這些數字,并把其中有意義的意思表達出來。
編程環境:
VS.NET
實現技術:
asp.net
關鍵:
String.Substring(Int32,Int32)方法的運用,Literal控件的使用,switch語句的使用。
正文:
在Web窗體上,放上一個Label控件,一個Literal控件,一個TextBox控件,一個Button控件。設置Label控件的Text屬性為“您的學號:”,Literal控件的Visible屬性設置為“False”。我主要是對Button控件的Click()事件進行編碼。當點擊一下按鈕后,對輸入的數字進行分析,然后把分析的內容用Literal控件顯示出來。
Button控件的Click()事件:
string studentNo = txtNo.Text; // 將學號賦給studentNo字符串
if (!studentInfo.Visible)
{
studentInfo.Visible = true; // 如果Literal控件是不可見的,則顯示它.
}
try
{
// 取子串操作
string strStartYear = studentNo.Substring(0,2); // 入學年份
string strTotalYears = studentNo.Substring(2,1); // 學制
string strSchool = studentNo.Substring(3,2); // 學院
string strClass = studentNo.Substring(5,1); // 班級
string strNumber = studentNo.Substring(6,2); // 號碼
// 將數字跟文字對應起來
// 內容純屬虛構
switch (strSchool)
{
case "01":
strSchool = "文學院";
break;
case "02":
strSchool = "理學院";
break;
case "03":
strSchool = "工學院";
break;
case "04":
strSchool = "科技學院";
break;
case "05":
strSchool = "傳播與藝術學院";
break;
case "06":
strSchool = "商學院";
break;
case "07":
strSchool = "法學院";
break;
case "08":
strSchool = "職教學院";
break;
case "09":
strSchool = "建工學院";
break;
case "10":
strSchool = "信息學院";
break;
default:
strSchool = "子虛烏有";
break;
}
studentInfo.Text = "您于"+strStartYear+"年入學"+",所選專業是"+strTotalYears+"年制的。"+
"您現在在"+strSchool+"學院"+strClass+"班學習"+",您的號碼是:"+strNumber+"。";
}
catch
{
Response.Write("取子串操作越界!");
}
finally
{
}
注:這里的學號是8位的。
舉例:
Web應用程序運行后,在文本框內輸入:02408122??纯唇Y果是什么?:)
效果圖:
對程序的擴展:
為了禁止輸入錯誤的內容,可以添加一個RegularExPRessionValidator和一個ValidationSummary控件,正則表達式為“/d{8}”,當輸入的不是8位數字,則會在頁面上顯示出錯信息。
摘要:
對身份證號碼的分析同對學號的分析類似。
正文:
這里認為身份證號是18位的。
在頁面上添加一個Label控件,一個TextBox控件,一個Button控件,一個Literal控件。Label控件的Text屬性設為“身份證號:”,Literal控件將顯示身份證號里的信息。關鍵還是在Button控件的Click()事件中。
Button控件的Click()事件:
string strID = txtID.Text;
if (!txtID.Visible)
{
txtID.Visible = true;
}
try
{
string strYear = strID.Substring(6,4); // 年
string strMonth = strID.Substring(10,2); // 月
string strDay = strID.Substring(12,2); // 日
Literal1.Text = "您的生日是:"+strYear+"年"+strMonth+"月"+strDay+"號";
}
catch
{
Response.Write("程序有錯誤!");
}
finally
{
}
顯示效果圖:
新聞熱點
疑難解答