




已阅读5页,还剩41页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#教程习题参考答案第一章(1) .NET Framework是平台,Visual Studio.NET是集成开发环境,C#是一种.NET平台下的开发语言(2) 易于掌握、支持跨平台、面向对象、与XML相融合(3) 组织资源、避免命名冲突(4) (5) 第二章上机练习(1) .(2)using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 上机练习2 class Program static void Main(string args) string myText; int Other = 0; int numLetters = 0; int numDigits = 0; char ch; int index = 0; Console.WriteLine(请输入要分析的字符串); myText = Console.ReadLine().ToUpper(); while (index myText.Length) ch = myTextindex; if (char.IsLetter(ch) numLetters+; else if (char.IsDigit(ch) numDigits+; else Other+; index+; Console.WriteLine(字符串分析结果为:); Console.WriteLine(字母有0个, numLetters); Console.WriteLine(数字有0个, numDigits); Console.WriteLine(其他支符有0个, Other); (3) using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 上机练习3 class Program class Rectangle private double x; private double y; public Rectangle(double x, double y) this.x = x; this.y = y; public double GetArea() return x * y; public double GetZhouChang() return (x + y) * 2; static void Main(string args) double a = 1.5; double b = 2.3; Console.WriteLine(长方形的周长是: + (a + b) * 2); Console.WriteLine(长方形的面积是: + a*b); Rectangle rt = new Rectangle(1.5, 2.3); Console.WriteLine(长方形的周长是: + rt.GetArea(); Console.WriteLine(长方形的面积是: + rt.GetZhouChang(); (4) using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1 class Program static void Main(string args) string str = Console.ReadLine();char ch = str.ToCharArray();/字符串转换为字符数组/输出转换结果foreach(char c in ch)Console.WriteLine(0,c);/实现反转char chtemp = str.ToCharArray();int longs = ch.GetLength(0);for(int i = 0 ; i = longs-1 ; i+)chtempi = chlongs-i-1;/使用修改后的字符数组构造新字符串string str2 = new string(chtemp);Console.WriteLine(str2); Console.ReadLine(); (5) 程序为:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 上机练习5 class Program class Employee private int id; public int Id get return id; set id = value; private string name; public string Name get return name; set name = value; private bool marry; public bool Marry get return marry; set marry = value; private decimal salary; public decimal Salary get return salary; set salary = value; public Employee(int id, string name, bool marry, decimal salary) this.id = id; = name; this.marry = marry; this.salary = salary; static void Main(string args) int id; string name; bool marry; decimal salary; Console.WriteLine(请输入员工的工号); id = int.Parse(Console.ReadLine(); Console.WriteLine(请输入员工姓名); name = Console.ReadLine(); Console.WriteLine(请输入员工婚否?(已婚请输入字母“y”;未婚请输入字母“n”); if (Console.ReadLine().ToLower().Equals(y) marry = true; else marry = false; Console.WriteLine(请输入员工工资); salary = Convert.ToDecimal(Console.ReadLine(); Object array = new Object1; array0 = new Employee(id, name, marry, salary); foreach (Employee item in array) Console.WriteLine(该员工的工号为: + item.Id); Console.WriteLine(该员工的姓名为: + item.Name); if (item.Marry = true) Console.WriteLine(该员工已婚); else Console.WriteLine(该员工未婚); Console.WriteLine(该员工的工资为: + item.Salary); 习题1、选择题(1) BD (2) D (3) ADE (4)ABC (5)ABD 2、填空题(1) -123 (2) delegate (3) 装箱 (4) n (5)堆内存 (6) 隐式转换和显式转换(7) ToCharArray(8) 编译错误:运算符“&”无法应用于“int”和“bool”类型的操作数; True;(9) ()3、简答题(1) 数据存放的位置与使用方式不同。(2) 装箱的过程首先创建一个引用类型的实例,然后将值类型变量的内容复制给该引用类型实例.(3) 变量名必须以字母开头; 变量名只能由字母、数字和下划线组成,而不能包含空格、标点符号、运算符等其他符号; 变量名不能与C#中的关键字名称相同; 变量名不能与C#的库函数名称相同。(4) 小数类型较浮点类型而言,具有更大的精确度,但是数值范围相对小了很多。将浮点类型的数向小数类型的数转化时会产生溢出错误,将小数类型的数向浮点类型的数转化时会造成精确度的损失。因此,两种类型不存在隐式或显式转换。(5) 在需要实例化出一个引用类型的对象时使用。第三章上机练习(1) .(2) 六行杨辉三角(3) 五行杨辉三角(4) using System;class Test public static int max(int a, int b, int c, int d) int tempmax = a; if (tempmax b) tempmax = b; if (tempmax c) tempmax = c; if (tempmax b) tempmin = b; if (tempmin c) tempmin = c; if (tempmin d) tempmin = d; return tempmin; public static void Main() Console.WriteLine(Please input four number:); int x1 = Convert.ToInt32(Console.ReadLine(); int x2 = Convert.ToInt32(Console.ReadLine(); int x3 = Convert.ToInt32(Console.ReadLine(); int x4 = Convert.ToInt32(Console.ReadLine(); Console.WriteLine(The Max Number is :0, max(x1, x2, x3, x4); Console.WriteLine(The Min Number is :0, min(x1, x2, x3, x4); Console.ReadLine(); (5) using System;class Test public static int addfor() int sum = 0; for(int i = 0 ; i = 50 ; i+ ) sum += i; return sum; public static int adddo() int sum = 0; int i = 0; do sum += i; i+; while (i = 50); return sum; public static int addwhile() int sum = 0; int i = 0; while (i = 50) sum += i; i+; return sum; public static void Main() Console.WriteLine(The Sum for is:0,addwhile(); Console.WriteLine(The Sum do is:0, addwhile(); Console.WriteLine(The Sum while is:0, addwhile(); Console.ReadLine(); 习题1、选择题(1) B (2) ACD (3) D (4) D (5) A 2、填空题(1) 顺序、分支、循环(2) break;reuturn;goto;continue(3) 3(4) 集合中元素的类型(5) 在C#语句和表达式的处理过程中激发了某个异常的条件,使得操作无法正常结束,引发异常、抛出异常3、简答题(1) 判断表达式一定要合法(2) 计算表达式的值;与分支进行匹配;执行分支语句;结束。(3) 提高程序的可读性和健壮性(4) Try块的代码是程序中可能出现错误的操作部分。Catch块的代码是用来处理各种错误的部分(可以有多个)。必须正确排列捕获异常的catch子句,范围小的Exception放在前面的catch。即如果Exception之间存在继承关系,就应把子类的Exception放在前面的catch子句中。Finally块的代码用来清理资源或执行要在try块末尾执行的其他操作(可以省略)。且无论是否产生异常,Finally块都会执行第四章上机练习(1) (2) using System;class MainClass public static void Main(string args) int sum = 0; int, , Score = 1, 2 , 3, 4 , 5,6 , 7,8 ; for (int i = 0; i 2; i+) for (int j = 0; j 2; j+) for (int k = 0; k 2; k+) sum += Scorei, j, k; Console.WriteLine(The sum is :0, sum); Console.ReadLine(); (3)using System;class MainClass public static void Main(string args) int, arr = new int3, 3; int i, j, t, m=0, k=0, flag = 0; for(i = 0 ; i 3 ; i +) for(j=0 ; j 3 ; j +) arri,j = Convert.ToInt32(Console.ReadLine(); /* 求出每一行的最大值,并判断它是否为所在列的最小值 */ for(i=0;i3;i+) t=arri,0; for(j=0;j3;j+) if(t=arri,j) t=arri,j; m=i; k=j; for(i=0;iarri,k) flag=1; /* 鞍点存在,查找是否有多个鞍点 */ if(flag=0) /* 找出鞍点个数并打印出来 */ for(j=0;j3;j+) if(arrm,j=arrm,k) /printf(the saddle point a%d%d= %dn,m,j,amj); Console.WriteLine(The saddle point a01=2n,m,j,arrm,j); else Console.WriteLine(there are no saddle point); Console.ReadLine(); (4) using System;class MainClass public static void Main(string args) int, arr = new int3, 3; int sum = 0; for (int i = 0; i 3; i+) for (int j = 0; j 0) ai = b % 2; b /= 2; i+; j+; Console.WriteLine(The result is :); for (k = j - 1; k = 0; k-) Console.WriteLine(0, ak); Console.ReadLine(); (6) using System;using System.Collections;using System.Collections.Generic;class friend string name=; string telephone=; string group=; public string Name get return name; set name = value; public string Telephone get return telephone; set telephone = value; public string Group get return group; set group = value; public friend(string n,string t,string g) name = n; telephone = t; group = g; public friend(string n) name = n; telephone = ; group = ; public void Display() Console.WriteLine(-); Console.WriteLine(姓名:0n,this.Name); Console.WriteLine(电话:0n,this.telephone); Console.WriteLine(组:0n,this.group); class telephonebook private ArrayList tele; public telephonebook() this.tele = new ArrayList(); public void Add(friend f) this.tele.Add(f); public void Delete(string na) friend temp=null; foreach (friend f in this.tele) if (friend)f).Name = na) temp = f; if(temp != null) this.tele.Remove(temp); public void Edit(string na) string name, tele, group; Console.WriteLine(请输入修改后的名称:); name = Console.ReadLine(); Console.WriteLine(请输入修改后的电话:); tele = Console.ReadLine(); Console.WriteLine(请输入修改后的组:); group = Console.ReadLine(); this.Delete(na); friend temp = new friend(name, tele, group); this.tele.Add(temp); public friend SearchByName(string na) foreach (friend f in this.tele) if (f.Name = na) return f; return null; public friend SearchByTele(string tel) foreach (friend f in this.tele) if (f.Telephone = tel) return f; return null; public void display() foreach (friend f in this.tele) f.Display(); class MainClass public static void Main(string args) telephonebook tb = new telephonebook(); friend zangsan = new friend(zs, 123, jiaren); zangsan.Display(); tb.Add(zangsan); friend temp = tb.SearchByTele(123); if (temp != null) temp.Display(); else Console.WriteLine(查无此人!); tb.display(); Console.ReadLine(); 习题1、选择题(1) D (2) C (3) A (4) ABC (5) C2、填空题(1) Sort() (2) 抛出异常、集合已修改;可能无法执行枚举操作。(3) 获取头部元素数据(4) Peek方法仅获取栈顶元素数据,Pop方法删除栈顶元素(5) 实现System.IEnumerable接口或实现集合模式(6) 名值对(7) SortedList中的Key数组排好序的第五章上机练习(1) (2) class student public string name; private int age; private int heigh; private string grade; public student() = ; this.age = 18; this.heigh = 180; this.grade = 0604; public student(string na, int a, int h, string g) this.grade = g; = na; this.heigh = h; this.age = a; student() (3)using System;using System.Collections.Generic;class Employeeprivate string name;private string ID; public static float TotalSalary; /定义实例构造函数,用来初始化实例中成员public Employee() = ;this.price = 0000; public Employee(string n,string i) = n; this.ID = i; /定义静态构造函数static Employee()TotalSalary = 0;/定义析构函数Employee()public void printName()Console.WriteLine();(4)using System;using System.Collections.Generic;class Employeeprivate string name;private string ID; public static float TotalSalary; private float Salary; /定义实例构造函数,用来初始化实例中成员public Employee() = ;this.ID = 0000; this.Salary = 0; public Employee(string n,string i,float s) = n; this.ID = i; this.Salary =s; TotalSalary += this.Salary; /定义静态构造函数static Employee()TotalSalary = 0;/定义析构函数Employee()public void printName()Console.WriteLine(); public static void DisplayTotalSalary() Console.WriteLine(总薪水为:0,TotalSalary); (5) using System;using System.Collections.Generic;class Account private string ID; private string PSW; private string Name; private float Balance; public static float TotalBalance; /定义实例构造函数,用来初始化实例中成员 public Account(string i, string p, string n, float b) this.Balance = b; this.ID = i; this.Name = n; this.PSW = p; TotalBalance += this.Balance; static Account() TotalBalance = 0; public void Deposits(int number) this.Balance += number; TotalBalance += number; public void Withdrawals(int number) if (this.Balance = number) Console.WriteLine(取款金额过多,请重试!); else this.Balance -= number; TotalBalance -= number; static void DisplayTotalBalance() Console.WriteLine(总余额为:0, TotalBalance); class MainClass public static void Main(string args) Account a1 = new Account(1, 1, 1, 222); Account a2 = new Account(2, 2, 2, 333); a1.Withdrawals(111); Account.DisplayTotalBalance(); Console.ReadLine(); 习题1、选择题(1) D (2) C (3) AB (4) CD (5) BC (6) D2、填空题(1) 封装、继承、多态(2) 编译错误,使用了未赋值的局部变量(3) 域、属性、方法、委托、事件、接口(4) public private protected internal(5) 当前类和其子类(6) 构造函数、类的方法和类的实例中3、简答题(1) 更容易描述现实世界,有利与开
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 茶叶电商运营与品牌战略合作合同
- 绿色节能厂房物业资产移交与能源管理服务合同
- 财务会计制度设计咨询合同
- 防洪堤彩钢板加固工程合同
- 恋爱合同兑现协议书模板
- 承包土地合同协议书图片
- 食材供货合同协议书范本
- 租房宠物合同协议书范本
- 代收水电暖合同协议书
- 仓库出租合同协议书下载
- 2023年高考全国甲卷数学(理)试卷【含答案】
- 2023年安徽ACM省赛试题
- 2023深圳一模数学试卷及答案
- 初中八年级红色文化课方志敏精神教案
- (完整版)METS医护英语水平考试
- 车险查勘定损中级培训水淹车处理指引及定损培训
- GB/T 25695-2010建筑施工机械与设备旋挖钻机成孔施工通用规程
- 纳米酶研究进展
- 力平之独特的血脂管理课件
- (完整版)土方回填专项施工方案
- 美容院卫生管理制度(常用版)
评论
0/150
提交评论