案例1:显示1至12月
步骤:
其代码如下;
{
Label1.Text = "";
for (int i = 1; i <= 12; i++)
{
Label1.Text += i + "月  "+"<br/>";
}
}
}
案例2:显示分类
步骤:
{
Label1.Text = "";
/*创建保存鲜花分r类列表的字符串型数组*/
string[] flower = { "红玫瑰", "康乃馨", "男色妖姬", "百合", "牵牛花" };
/*创建保存鲜花列表对应的网页地址的字符串型数组*/
string[] url = {"http://yueyuanyuan.blog.51cto.com","http://www.baidu.com","http://www.sina.com",
"http://www.hao123.com","http://www.it168.com"};
for (int i = 0; i < flower.Length; i++)//flower.Length获取数组长度
{
Label1.Text += "<a href=" + url[i] + ">" + flower[i] + "</a><br/>";
}
}
}
案例3:显示日记
步骤:
其代码为;
{
if (!IsPostBack)//页面第一次用一下
{
ViewState["index"] = 0;//默认为var类型(什么都可以放)
ViewState["riji"] = new string[31];
}
}
//属性 类就是属性和方法的集合 书:C#程序语言
//结构->类 // 什么时候用结构,什么时候用类。
//windows程序是由“事件”组成
public string[] riji
{
get { return (string[])ViewState["riji"]; } // 取出
set { ViewState["riji"] = value; }//value(一个值,什么都可以命名)
}
public int index
{
get { return (int)ViewState["index"]; }
set { ViewState["index"] = value; }
}
protected void Button1_Click(object sender, EventArgs e)
{
//做什么?
//事件内、类、属性,方法中程序全是顺序结构处理的
Label1.Text = "";
if (TextBox1.Text != "")
{
if (index < riji.Length)
{
/* riji[index] = "时间:" + DateTime.Now.ToString() + " " +"日记内容是:" + TextBox1.Text;*/
riji[index] = "时间:" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月" + DateTime.Now.Date + "日" + " " + "日记内容是:" + TextBox1.Text;
index++;
Label1.Text = "添加日记成功!";
TextBox1.Text = "";
}
else
{
Label1.Text = "一个月最多有31天";
}
}
else
{
Label1.Text = "请输入日记内容";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "";
foreach (string xianshi in riji)
{
if (xianshi != null)
{
Label1.Text = Label1.Text + xianshi + "</br>";
}
}
}
}
案例4:显示中×××号码
步骤:
其代码为;
{
int[] benqi = {3,7,19,25,27,30,33};
int[] maicaipiao = {2,7,19,21,27,30,33};
int num = 0; //中了几个号
Label1.Text="你中了";
for (int i = 0; i < maicaipiao.Length; i++)
{
for (int j = 0;j < benqi.Length;j++)
{
if (maicaipiao[i] == benqi[j])
{
num++;
Label1.Text += maicaipiao[i] + ",";
}
}
}
Label1.Text += num.ToString() + "个号,真牛逼啊!";
}
}
案例4:显示栏目分类
步骤:
其代码为;
{
{
int[] mylist = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
for (int i = 0; i <mylist.Length; i++)
{
if ((i % 5) ==0)
{
Label1.Text +="</br>";
}
Label1.Text += mylist[i].ToString() + " ";
}
}
}
}
案例5:显示最大的数
步骤:
其代码为;
{
int[] mylist = { 1, 2, 22, 4, 5, 23, 7, 88, 9, 10, 36, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int max = 0;
for (int i = 1; i < mylist.Length; i++)
{
if (mylist[i] < mylist[i - 1])
{
max = mylist[i - 1];
mylist[i - 1]=mylist[i];
mylist[i]= max;
}
max = mylist[i];
}
Label1.Text = max.ToString();
}
}
案例5:从小到大排列数
步骤:
其代码为;
{
int[] mylist = { 1, 2, 3, 14, 5, 6, 17, 8, 9, 10, 11, 12, 13, 4, 15, 16, 7, 18, 19,20};
int max = 0;
for (int i = 1; i < mylist.Length; i++)
{
for (int j = 1; j < (mylist.Length - i); j++)
{
if (mylist[j] < mylist[j - 1])
{
max = mylist[j - 1];
mylist[j - 1] = mylist[j];
mylist[j] = max;
}
}
}
foreach (int j in mylist)
{
Label1.Text += j.ToString() + " ";
}
}
}
转载于:https://blog.51cto.com/yueyuanyuan/319103