c#程序设计基础-1.ppt_第1页
c#程序设计基础-1.ppt_第2页
c#程序设计基础-1.ppt_第3页
c#程序设计基础-1.ppt_第4页
c#程序设计基础-1.ppt_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于.net的Web程序设计,主讲人 任建平 电子与信息工程学院,第2章 ASP.NET 3.5概述,例1 利用If语句和For语句实现对字符串的分类统计功能,案例描述 任意输入一个字符串,统计字符串中含有的大写字母、小写字母、数字字符、空格和其他字符的个数。,在运行时从文本框读取一个任意输入的字符串。使用for语句对每个字符进行处理。 在循环体中,使用if语句对字符的类型进行判断。在网页上显示统计的结果。,(一)案例分析,(二) 操作步骤,1. 建立字符串分类统计网站 “文件” “新建” “网站” (ASP.NET网站模板,语言为C#以及位置) 2. 建立字符串分类统计网页程序 在“解决方案资源管理器”的项目名称上右键选择“添加新项”,在添加新项对话框中选择“Web窗体”,文件名命名为“count.aspx”,将该文件设为“”起始页。,3. 编辑“分类统计” protected void countClick(object sender, EventArgs e) string inStr = “; Char inChar; int i, len = 0, intCount1, intCount2, intCount3, intCount4, intCount5; intCount1 = intCount2 = intCount3 = intCount4 = intCount5 = 0; inStr = Text_instr.Text; /读取一个任意输入的字符串 len = inStr.Length; /取得字符串的长度 for (i = 0; i 小写字母的个数:“ + intCount2.ToString(); Response.Write(“数字字符的个数:“ + intCount3.ToString(); Response.Write(“ 空格的个数:“ + intCount4.ToString(); Response.Write(“其他字符的个数:“ + intCount5.ToString(); 4. 保存、编译、执行,(三) 本例知识点,1. 知识点 1)选择结构If语句 2)循环结构for语句 3)变量 4)字符和字符串,2. 设计技巧 1)同时为多个变量赋值 变量1 = 变量2 = 变量3 = 0 ; 2)将字符串中的某个字符赋值给字符变量 inChar = Convert.ToChar(inStr.Substring(i, 1);,(三) 本例知识点,例2 利用数组完成对客户信息的输入和统计功能,案例描述 任意输入3名客户的信息,包括客户号、客户名称和所属行业,将这些信息保存在数组中。 其中,所属行业为1代表IT行业,为2代表金融行业,为3代表运输行业,为4代表建筑行业,其他值表示行业未知。 对所有客户的所属行业情况分别进行统计。,本案例需要定义一个结构类型来表示客户信息中的各种数据。 所有客户的信息保存在数组中,数组的数据类型为该结构类型。 使用do-while循环语句显示数组中保存的每个客户的信息。 使用foreach循环语句对每个数组元素中的客户所属行业进行判断,分别累加,得到统计结果。,(一)案例分析,(二) 操作步骤,1. 建立客户情况统计网站 “文件” “新建” “网站” 2. 建立客户情况统计网页程序 新建“Web窗体”名为“customInfo.aspx”并设为起始页 3. 声明客户信息结构类型 struct Custom /客户信息 public String ID; /客户号 public String Name; /客户名称 public int Job; /所属行业 1.IT 2.金融 3.运输 4.建筑 其他.未知 ;,4. 编辑客户情况统计按钮的事件处理代码 protected void cusCount(object sender, EventArgs e) const int NUMBER = 3; /定义常量,设置数组元素的个数 int job, job1, job2, job3, job4, job5, count; Custom structCustom = new CustomNUMBER; /声明和创建客户信息数组 job = job1 = job2 = job3 = job4 = job5 = 0; structCustom0.ID = Text_ID1.Text; structCustom0.Name = Text_Name1.Text; job = Convert.ToInt32(Text_Job1.Text); structCustom0.Job = job; structCustom1.ID = Text_ID2.Text; structCustom1.Name = Text_Name2.Text; job = Convert.ToInt32(Text_Job2.Text); structCustom1.Job = job; structCustom2.ID = Text_ID3.Text; structCustom2.Name = Text_Name3.Text; job = Convert.ToInt32(Text_Job3.Text); structCustom2.Job = job; count = 1; /显示全部客户信息 do Response.Write(“第“ + count.ToString() + “个客户:客户号=“ + structCustomcount - 1.ID + “ 客户姓名=“ + structCustomcount - 1.Name + “ 客户所属行业=“ + structCustomcount - 1.Job + “); count = count + 1; while (count 属于 金融行业 的客户人数:“ + job2.ToString(); Response.Write(“属于 运输行业 的客户人数:“ + job3.ToString(); Response.Write(“属于 建筑行业 的客户人数:“ + job4.ToString(); Response.Write(“ 行业未知 的客户人数:“ + job5.ToString(); 5. 保存、编译、执行,(三) 本例知识点,1. 知识点 1)数组 2)结构类型 3)foreach语句 4)do-while语句 5)switch语句,(三) 本例知识点,2. 设计技巧 1)获取数组的长度 string name = new string “Jessy“, “Bill“, “Jack“ ; len = name.Length; 2)获取数组的最大/最小下标 Max = name.GetUpperBound(0); Min = name.GetLowerBound(0); 3)数据类型转换 使用Convert类中的方法。 使用Parse()方法。,例3 采用面向对象编程技术开发小型活期储蓄业务系统,案例描述 该系统的主要功能有:活期储蓄帐户的开户、存款、取款和查询余额。 帐户信息中包括帐号、储户姓名、开户日期、业务日期、发生额、业务种类和余额。 业务种类为1表示开户,2表示存款,3表示取款。 要求每个帐户在未销户前的余额不能小于1元。,(二)案例分析,创建一个帐户类Account,其字段包括帐号、储户姓名、开户日期、业务日期、发生额、业务种类和余额。 在Account类中添加方法:newAccount()、Deposit()、Withdraw()、和getBalance(),分别实现开户、存款、取款和查询余额功能。 当开户和取款业务中出现余额小于1元时要抛出异常,提示错误信息。,(三) 操作步骤,1. 建立小型活期储蓄业务系统网站 “文件” “新建” “网站” 2. 建立活期储蓄网页程序 新建“Web窗体”名为“coreBanking.aspx”并设为起始页 3. 编辑帐户类Account public class Account /帐户类 /以下为帐户类的属性 private int accID; /帐号 private string Name; /储户姓名 private DateTime khDate; /开户日期 private int Type; /业务种类.为1表示开户,2表示存款,3表示取款 private DateTime ywDate; /业务日期 private decimal Money; /发生额 private decimal Balance; /余额,4. 编辑帐户类Account的newAccount()方法 public int newAccount(int inID, string inName, decimal inMoney) /开户方法 try if (inMoney (decimal)1.00) throw new Exception(); /当开户金额小于1元时抛出异常 this.accID = inID; this.Name = inName; this.khDate = DateTime.Now; /取系统日期; this.Type = 1; this.ywDate = DateTime.Now; /取系统日期; this.Money = inMoney; this.Balance = inMoney; return 0; catch (Exception ex) /捕获异常 return -1; ,5. 编辑帐户类Account的Deposit()方法 public void Deposit(decimal myInMoney) /存款方法 this.Type = 2; this.ywDate = DateTime.Now; this.Money = myInMoney; this.Balance = this.Balance + myInMoney; 6. 编辑Account类的Withdraw()方法 public int Withdraw(decimal myInMoney) /取款方法 try if (this.Balance - myInMoney) (decimal)1.00) throw new Exception(); /当取款后余额小于1元时抛出异常 this.Type = 3; this.ywDate = DateTime.Now; this.Money = myInMoney; this.Balance = this.Balance - myInMoney; return 0; catch (Exception ex) /捕获异常 return -1; ,7. 编辑帐户类Account的getBalance()方法 public decimal getBalance() /查询余额方法,该方法返回帐户余额 return this.Balance; 编辑活期储蓄按钮的事件处理代码 protected void bankClick(object sender, EventArgs e) int inID,result; string inName; decimal inMoney, outMoney, balance; inID = Convert.ToInt32(Text_id.Text); inName = Text_name.Text; inMoney = Convert.ToDecimal(Text_money.Text);,Account myAccount = new Account(); /创建Account类的对象 result=myAccount.newAccount(inID, inName, inMoney); /调用myAccount对象的开户方法 if (result = -1) Response.Write(“开户交易不成功!开户金额不能小于1元!“); Response.End(); balance = myAccount.getBalance(); /调用myAccount对象的查询余额方法,返回帐户余额 Response.Write(“开户后的余额=“ + balance.ToString() + “); inMoney = Convert.ToDecimal(Text_inMoney.Text); myAccount.Deposit(inMoney); /调用myAccount对象的存款方法 balance = myAccount.getBalance(); Response.Write(“存款后的余额=“ + balance.ToString()+“); outMoney = Convert.ToDecimal(Text_outMoney.Text); result=myAccount.Withdraw(outMoney); /调用myAccount对象的取款方法 if (result = -1) Response.Write(“取款交易不成功!取款后余额不能小于1元!“

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论