ASP.NET--循环结构(实战案例)

案例1:显示1至12月
步骤:
其代码如下;
{
Label1.Text = "";
for (int i = 1; i <= 12; i++)
{
Label1.Text += i + "月&nbsp;&nbsp"+"&lt;br/>";
}
}
}
clip_image002
clip_image003
clip_image004
clip_image005
案例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 += "&lt;a href=" + url[i] + ">" + flower[i] + "</a><br/>";
}
}
}
clip_image006
clip_image007
clip_image008
clip_image009
案例3:显示日记
步骤:
clip_image010
其代码为;
{
if (!IsPostBack)//页面第一次用一下
{
ViewState["index"] = 0;//默认为var类型(什么都可以放)
ViewState["riji"] = new string[31];
}
}
//属性 类就是属性和方法的集合 书:C#程序语言
//结构-&gt;类 // 什么时候用结构,什么时候用类。
//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() + "&nbsp;&nbsp;&nbsp;&nbsp;" +"日记内容是:" + TextBox1.Text;*/
riji[index] = "时间:" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月" + DateTime.Now.Date + "日" + "&nbsp;&nbsp;&nbsp;&nbsp;" + "日记内容是:" + 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 + "&lt;/br>";
}
}
}
}
clip_image011
clip_image012
clip_image013
clip_image014
clip_image015
案例4:显示中彩票号码
步骤:
clip_image016
其代码为;
{
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 &lt; benqi.Length;j++)
{
if (maicaipiao[i] == benqi[j])
{
num++;
Label1.Text += maicaipiao[i] + ",";
}
}
}
Label1.Text += num.ToString() + "个号,真牛逼啊!";
}
}
clip_image017
clip_image018
案例4:显示栏目分类
步骤:
clip_image019
其代码为;
{
{
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 &lt;mylist.Length; i++)
{
if ((i % 5) ==0)
{
Label1.Text +="&lt;/br>";
}
Label1.Text += mylist[i].ToString() + "&nbsp";
}
}
}
}
clip_image020
clip_image021
案例5:显示最大的数
步骤:
clip_image022
其代码为;
{
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 &lt; mylist.Length; i++)
{
if (mylist[i] &lt; mylist[i - 1])
{
max = mylist[i - 1];
mylist[i - 1]=mylist[i];
mylist[i]= max;
}
max = mylist[i];
}
Label1.Text = max.ToString();
}
}
clip_image023
clip_image024
案例5:从小到大排列数
步骤:
clip_image025
其代码为;
{
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 &lt; mylist.Length; i++)
{
for (int j = 1; j &lt; (mylist.Length - i); j++)
{
if (mylist[j] &lt; mylist[j - 1])
{
max = mylist[j - 1];
mylist[j - 1] = mylist[j];
mylist[j] = max;
}
}
}
foreach (int j in mylist)
{
Label1.Text += j.ToString() + "&nbsp;";
}
}
}
clip_image026
clip_image027

你可能感兴趣的:(net,asp,实战,结构,休闲)