2014年1月19日 星期日

流程控制

1.if條件判斷式 

  • 條件成立才執行
    • 使用者輸入在文字欄位輸入帳號時,按下button時就把輸入值印出來
範例:
    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(TextBox1.Text) == false)
            {
                Response.Write("登入者:" + TextBox1.Text);
            }
        }
    }

    • 透過if(string.IsNullOrEmpty(TextBox1.Text) == false),檢查TextBox1.Text是否為空,
    • 如果不為空才執行Response.Write("登入者:" + TextBox1.Text);
  • 判斷是否為貨幣
    • 使用if及decimal.TryParse函式


    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            decimal d;//錢幣的宣告
            if(decimal.TryParse(TextBox1.Text,out d))
            {
                Response.Write(d.ToString("c"));//數值的格式化 decimal
            }
        }
    }
  • 二選一的情況
    • 沒輸入帳號就提示"請輸入帳號"


    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(TextBox1.Text) == false)
            {
                Response.Write("帳號為" + TextBox1.Text);
            }
            else
            {
                Response.Write("請輸入帳號");
            }
        }
    }

    • 練習輸入可預約日期(大於今日皆可預約)

    
        protected void Button2_Click(object sender, EventArgs e)
        {
            DateTime dt;
            if (DateTime.TryParse(TextBox1.Text, out dt))
            {
                if (dt > DateTime.Now)
                {
                    Response.Write(dt.ToLongDateString().ToString()+"可以預約");
                }
                else
                {
                    Response.Write("請輸入未來日期");
                }
            }
            else
            {
                Response.Write("請輸入正確日期格式");
            }
        }

      • DateTime.TryParse(TextBox1.Text, out dt)檢查是否為正確日期格式
      • dt > DateTime.Now檢查是否大於今日
      • dt.ToLongDateString().ToString()日期格式化為2014年2月23日
  • 多選一的情況
    • TextBox1.Text的內容是否大於0
    
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(TextBox1.Text)>0)//Convert.ToInt32轉換成int型別
            {
                Response.Write("正數");
            }
            else if(Convert.ToInt32(TextBox1.Text) == 0)//Convert.ToInt32轉換成int型別

            {
                Response.Write("零");
            }
            else
            {
                Response.Write("負數");
            }
        }

2.switch條件判斷

  • switch判斷式有兩個地方需要注意
    • 運算式的結果必須是整數(sbyte ,byte ,short ,ushort ,int ,uint ,long ,ulong)、列舉型態資料、字串(char)、字串(string)。
    • 每一個case敘述最後必須要加上break,表示離開switch判斷式。

    
        protected void Button2_Click(object sender, EventArgs e)
        {
            int month = DateTime.Today.Month;
            Response.Write("現在的月份:" + month + " ; ");
            switch(month)
            {
                case 1:
                    Response.Write("這個月有31天");
                    break;
                case 2:
                    Response.Write("這個月有28天");
                    break;
                default:
                    Response.Write("其他月份");
                    break;
            }
        }
結果:
現在的月份:1 ; 這個月有31天

3.foreach迴圈

  • foreach(型別 變數 in 集合或陣列名稱)
    {
            //重複執行的程式
    }
範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            int[] lottos = { 10,24,33,7,16,11};
            foreach(int lotto in lottos)
            {
                Response.Write("樂透號碼為:" + lotto + " ; ");
            }
        }
結果:
樂透號碼為:10 ; 樂透號碼為:24 ; 樂透號碼為:33 ; 樂透號碼為:7 ; 樂透號碼為:16 ; 樂透號碼為:11 ;
範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] Books = { "ASP.NET", "ADO.NET", "C#" };
            foreach (string book in Books)
            {
                Response.Write("樂透號碼為:" + book + " ; ");
            }
        }
結果:
樂透號碼為:ASP.NET ; 樂透號碼為:ADO.NET ; 樂透號碼為:C# ; 

4.例外處理

  • try
    {
            //程式碼
    }
    catch
    {
           //例外處理程式碼
    }
範例:
    
            try
            {
                int en = int.Parse(TextBox1.Text);
                int code = int.Parse(TextBox2.Text);
                int total = en + code;
                Label2.Text = total.ToString();
            }
            catch(Exception ex)
            {
                Label2.Text = ex.Message;
            }
結果:
  • 也可自訂例外訊息
範例:
    
            try
            {
                int en = int.Parse(TextBox1.Text);
                int code = int.Parse(TextBox2.Text);
                int total = en + code;
                Label2.Text = total.ToString();
            }
            catch(Exception ex)
            {
                Label2.Text = "系統忙碌中....";
            }

  • 識別不同的例外
    • 通常使用者輸入一個無法轉成整數的值,像是abc、2008/01/06或是輸入超出int允許的範圍,則會發生溢位(Overflow)的錯誤,所以必須針對不同的例外顯示不同的錯誤訊息或是給予不同的錯誤處理方式
範例:
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                int en = int.Parse(TextBox1.Text);
                int code = int.Parse(TextBox2.Text);
                int total = en + code;
                Label2.Text = total.ToString();
            }
            catch(FormatException fex)
            {
                Label2.Text = "請輸入整數資料";
            }
            catch (OverflowException oex)
            {
                Label2.Text = "資料過大造成錯誤";
            }
        }
  • 一般的Excption要放在最後

    
            try
            {
                int en = int.Parse(TextBox1.Text);
                int code = int.Parse(TextBox2.Text);
                int total = en + code;
                Label2.Text = total.ToString();
            }
            catch(FormatException fex)
            {
                Label2.Text = "請輸入整數資料";
            }
            catch (OverflowException oex)
            {
                Label2.Text = "資料過大造成錯誤";
            }
            catch(Exception ex)
            {
                Label2.Text = "系統忙碌中...";
            }

沒有留言:

張貼留言