循环结构

第三章        循环结构
 
案例一:显示112月份
 
  Label1.Text = "";
        for (int i = 1; i <= 12; i++)
        {
        }
 
            Label1.Text += i + " &nbsp;&nbsp;" ;
 
 
案例二:显示鲜花分类
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";// 创建保存鲜花分类列表的字符串型数组
        string[] flower = { " 红玫瑰" , " 百合" , " 蓝色妖姬" , " 康乃馨" , " 手提花篮" };
        string[] str= {"rose.aspx","lily.aspx","blue.aspx","carnation.aspx","basker.aspx"};
     for (int i=0; i<flower.Length; i++)// 使用for循环显示列表
     {
         Label1.Text+="<a href="+str[i]+">"+flower[i]+"</a><br/>";
     }
      
    }
}
 
 
案例三:显示日记
后台代码编写:
        if (!IsPostBack)
        {
            ViewState["index"] = 0;
            ViewState["riji"] = new string[31];
        }
    }
    public string[] riji
    {
        get { return (string[])ViewState["riji"]; }
        set { ViewState["riji"] = 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;
                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>";
            }
        }
    }
 
 
 
 
 
 
 
 
(3)进行验证:
点击添加日记,添加成功!
 
点击显示日记,显示成功!
 
点击清空显示,清空成功!
 
如果点击重写日记,那将会删除以前写过的日记:
 
案例四: 循环的中断
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < 100)// 循环i<100的数
        {
            if (i == 16)// 判断当i等于16
                break;// 中断循环
            Label1.Text += Convert.ToString(i) + "&nbsp;&nbsp;";
            i++;
        }
    }
}
 
案例五:查询中奖号码
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = " 本注彩票号码为:" ;
        Label2.Text = " 本期中奖号码为:" ;
        int[] num = { 3, 7, 19, 25, 27, 30, 33 };// 彩票的号码
        int[] win = { 3, 22, 66, 25, 90, 30, 33 }; // 中将的号码
        int k = 0; // 计算中奖 号码个数计数器
        int m = 0; // 彩票的号码
        int n = 0; // 中将的号码
        while (m < num.Length)
        {
            Label1.Text += num[m] + "&nbsp;&nbsp;";
            m++;
        }
        while (n < win.Length)
        {
            Label2.Text += win[n] +"&nbsp;&nbsp;";
            n++;
        }
        for (int i = 0; i < num.Length; i++)
        {
            for (int j = 0; j < win.Length; j++)
            {
                if (num[i] == win[j])
                {
                    k++;
                }
             
            }
        }
 
        switch (k)
        {
            case 7:
            TextBox1 .Text  = " 恭喜你!中了一个笔记本" ;
                break ;  
            case 6:
          TextBox1 .Text  = " 恭喜你中了一个手机" ;
                break ;   
            case 5:
            TextBox1 .Text  = " 恭喜你中了一个MP3" ;
                break ;       
            case 4:
            TextBox1 .Text  = " 恭喜你中了一个U" ;
                break ;
            default :   
        TextBox1 .Text  = " 恭喜你什么都没中" ;
         break;
       
        }
      
    }
}
 
 

本文出自 “曹宏江--著” 博客,转载请与作者联系!

你可能感兴趣的:(职场,结构,休闲)