ASP.NET--程序异常处理(实战案例)

案例1:实现在线计算器
步骤:
ASP.NET--程序异常处理(实战案例)_第1张图片
clip_image002
clip_image003
其代码为;
{
try
{
double a = Convert.ToDouble(TextBox1.Text);
double b = Convert.ToDouble(TextBox2.Text);
if (DropDownList1.SelectedValue == "加")
{
TextBox3.Text = Convert.ToString(a + b);
}
else if (DropDownList1.SelectedValue == "减")
{
TextBox3.Text = Convert.ToString(a - b);
}
else if (DropDownList1.SelectedValue == "乘")
{
TextBox3.Text = Convert.ToString(a * b);
}
else if (DropDownList1.SelectedValue == "除")
{
if (b != 0)
{
TextBox3.Text = Convert.ToString(a / b);
}
else
{
TextBox3.Text = "不能除以0,你不知道?";
}
}
}
catch
{
TextBox3.Text = "请输入数字";
}
}
}
clip_image004
clip_image005
clip_image006
clip_image007
clip_image008
clip_image009
clip_image010
案例2:显示乘法口诀
步骤:
clip_image011
其代码为;
数字型:
{
        int sum = 0;
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
               sum = i * j;
                Label1.Text = Label1.Text + j.ToString() + "x" + i.ToString() + "=" + sum.ToString() + "&nbsp;";
            }
            Label1.Text += "</br>";
        }
    }
}
clip_image012
clip_image014
汉字型:
 {
      int sum = 0;
      string s1;
      string s2;
      string[] yezi = { "十", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
      for (int i = 1; i <= 9; i++)
      {
          for (int j = 1; j <= i; j++)
          {
              sum = i * j;
              if (sum < 10)
              {
                  Label1.Text = Label1.Text + yezi[j] + yezi[i] + "得" + yezi[sum] + " &nbsp;";
              }
              else
              {
                  if (sum % 10 != 0)
                  {
                      s1 = sum.ToString().Substring(0, 1);
                      s2 = sum.ToString().Substring(1, 1);
                      Label1.Text = Label1.Text + yezi[j] + yezi[i] + yezi[Convert.ToInt32(s1)] + "十" + yezi[Convert.ToInt32(s2)] + "&nbsp;";
                  }
                  else
                  {
                      s1 = sum.ToString().Substring(0, 1);
                      s2 = sum.ToString().Substring(1, 1);
                      Label1.Text = Label1.Text + yezi[j] + yezi[i] + yezi[Convert.ToInt32(s1)] + yezi[Convert.ToInt32(s2)] + " &nbsp;";
                  }
              }
          }
          Label1.Text += "</br>";
      }

    }
}
clip_image016

本文出自 “乐成的技术笔记” 博客,谢绝转载!

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