




已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
-WORD格式-可编辑-C#应用开发技术习题1.用 enum定义字节类型的方位常量, 打印出某一方位并将此方位值转化为字节类型, 字符串类型值。分析输出结果的原因。 回答以下问题:Enum的缺省类型是什么?直接输出myDirection 和(byte)myDirection 有何区别。class Variablesenum orientation : bytenorth = 1,south = 2,east = 3,west = 4static void Main(string args)byte directionByte;string directionString;orientation myDirection = orientation.north;Console.WriteLine(myDirection = 0, myDirection);directionByte = (byte)myDirection;directionString = Convert.ToString(myDirection);Console.WriteLine(byte equivalent = 0, directionByte);Console.WriteLine(string equivalent = 0, directionString);Console.ReadLine();2建立使用关系运算符和逻辑运算符的程序文件。Main 方法中实例代码如下static void Main(string args)Console.WriteLine(Enter an integer:);int myInt = Convert.ToInt32(Console.ReadLine();Console.WriteLine(Integer less than 10? 0, myInt 10);Console.WriteLine(Integer between 0 and 5? 0,(0 = myInt) & (myInt = 5);Console.WriteLine(Bitwise AND of Integer and 10 = 0, myInt & 10);Console.ReadLine();编译运行该程序。并尝试myInt 输入不同范围整数,非10和10时的输出差异。3 .从键盘输入两个数进行比较, 并定义一个字符串变量, 当数 1小于数 2时,字符串变量为 “ less than ”,当当数 1等小于数 2时字符串变量为 “ equalto ”,当数 1大于数 2时字符串变量为 “ greater than ”。static void Main(string args)-string comparison;Console.WriteLine(Enter a number:);double var1 = Convert.ToDouble(Console.ReadLine();Console.WriteLine(Enter another number:);double var2 = Convert.ToDouble(Console.ReadLine();if (var1 var2)comparison = less than;elseif (var1 = var2)comparison = equal to;elsecomparison = greater than;Console.WriteLine(The first number is 0 the second number., comparison);Console.ReadLine();4.定义三个常量字符串“ karli,angelina” ,ploppy ,并从键盘输入一个名字,当名字与 “ karli”相同时输出我们的名字相同,当和angelina 名字相同时输出你的名字太性感了,当和ploppy 相同时输出这名字真傻。static void Main(string args)const string myName = karli;const string sexyName = angelina;const string sillyName = ploppy;string name;Console.WriteLine(What is your name?);name = Console.ReadLine();switch (name.ToLower()case myName:Console.WriteLine(You have the same name as me!);break;case sexyName:Console.WriteLine(My, what a sexy name you have!);break;case sillyName:Console.WriteLine(Thats a very silly name.);break;Console.WriteLine(Hello 0!, name);Console.ReadLine();- 2 -5 for 循环语句练习(1) 程序功能要求:按5 度的增量打印出一个从摄氏温度到华氏温度的转换表。static void Main(string args)double Fa,Cel;Cel =0;for(Cel=0;Cel100;Cel+=5)Fa = Cel * 9/5;Console.WriteLine(Fa);Console.ReadLine();(2)自行改造以上程序。6. while循环语句练习( 1)程序功能要求:运行程序后从键盘输入数字1/2/3 后,可显示抽奖得到的奖品:恭喜你得了一辆汽车;不错啊,一台笔记本电脑;没白来,一个MP3 ;如果输入其它数字或字符显示 “没有奖品给你! ”。示例代码如下:int choice;choice = Convert.ToInt32(Console.ReadLine();while (choice = 1)Console.WriteLine( 恭喜你得了一辆汽车);break;while (choice = 2)Console.WriteLine( 不错啊,一台笔记本电脑);break;while (choice = 3)Console.WriteLine( 没白来,一个MP3);break;while (choice != 1 & choice != 2 & choice != 3)Console.WriteLine( 没有奖品给你);break;( 2)改造以上程序实现此功能;尝试将 choice=1 或 2或 3中的 “=”改为一个 “=”,看效果如何?并分析错误。- 3 -7 do while 循环语句练习程序功能要求: 输入你现有的存款和当前的年利率及你期望将来得到的存款,计算出存款多少年后才可以变成你期望的存款额。注意,若为一年输出year为 year,若为多年输出year为years。参考代码如下:static void Main(string args)double balance, interestRate, targetBalance; Console.WriteLine(What is your current balance?); balance = Convert.ToDouble(Console.ReadLine(); Console.WriteLine(What is your current annual interest rate (in %)?); interestRate = 1 + Convert.ToDouble(Console.ReadLine() / 100.0; Console.WriteLine(What balance would you like to have?); targetBalance = Convert.ToDouble(Console.ReadLine();int totalYears = 0;dobalance *= interestRate;+totalYears;while (balance targetBalance);Console.WriteLine(In 0 year1 youll have a balance of 2., totalYears, totalYears = 1 ? : s, balance);8使用 if.else 语句编写以下程序( 1)程序功能要求:使用 if.else 语句构造多分支,判断某一年是否为闰年。闰年的条件是符合下面二者之一:能被 4 整除,但不能被 100 整除;能被 4 整除,又能被 100 整除。9使用 switch 语句编写以下程序在不同温度时显示不同的解释说明:有点冷,多穿衣服;正合适,出去玩吧;太热了,开空调。10. 用 do while 语句实现程序功能:求 1 2 +100 之和,并将求和表达式与所求的和显示出来。11. 定义一个圆类,计算圆的面积和周长public class circlepublic static void Main()double radium, delimeter, square;const double pai = 3.1415926;radium = Convert.ToInt32(Console.ReadLine();delimeter = 2 * pai * radium;- 4 -square = pai * pai * radium;Console.WriteLine(delimeter=0,square=1, delimeter, square);Console.ReadLine();或者:public class circledoubledelimeter, square;const double pai = 3.1415926;public void calculate(double rad)delimeter = 2 * pai * rad;square = pai * pai * rad;Console.WriteLine(delimeter=0,square=1,delimeter,square);public static void Main()double radium;circle cir = new circle();radium = Convert.ToInt32(Console.ReadLine();cir.calculate(radium);Console.ReadLine();请比较以上两个程序,看起来后一个程序把问题复杂化了,是不是不如第一个程序好,它从设计思想上有什么优势么?12. 程序要求如下:其中有 3 个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这个类, 实现对学生数据的赋值和输出。 要求: 使用成员函数实现对数据的输出;使用构造函数实现对数据的输入。参考代码如下:public class studentsstringid,name;int age;public students(string id,string name,int age )this.id = id; = name;- 5 -this.age = age;public void Display()Console.WriteLine(id=0,name=1,age=2,id,name,age);public static void Main()/string id, name;/int age;students stu = new students(0001,zhangsan,16);stu.Display();Console.ReadLine();以上程序使用了构造方法,请回答关键字this 有何作用,你能将成员函数Display 修改成别的代码也实现响应的功能么?13. 编写帐户类, 对每一账号赋值帐户并设置初始化存款为0.00 元,设计一变量统计账号生成的数目。public class BankAccountstatic int totalAccountNumber=0;string BankAccountId;double initialDepositAmount = 0.00;public BankAccount(string myId)this.BankAccountId = myId;this.initialDepositAmount = 0.00;totalAccountNumber+;public void displayid()Console.WriteLine(mbaid=0,initialDepositAmount=1,this.BankAccountId,this.initialDepositAmount);public static void display()Console.WriteLine(totalAccountNumber=0, totalAccountNumber);- 6 -public class Testerpublic static void Main()BankAccount mba = new BankAccount(37000001);BankAccount mba2 = new BankAccount(3700002);BankAccount mba3 = new BankAccount();BankAccount mba4 = new BankAccount(3700004);/ Console.WriteLine(mba2ID=0, mba2.BankAccountId); mba2.displayid();BankAccount.display();Console.ReadLine();请回答问题:( 1)按你自己的算法修改以上程序,比如可只输出生成的账户数。( 2)把注释去掉后会怎样,为什么?( 3)为什么 display 用类名直接引用,可以用对象来引用么?尝试输出结果。( 4)类的静态变量和非静态变量的引用区别。判断一下语句的正确性:静态方法只能使用静态变量,不能使用实例变量。因为对象实例化之前,实例变量不可用。这个观点真确么?()类的静态变量只有一个版本,所有实例对象引用的都是同一个版本。()对象实例化后,每个实例变量都被制作了一个副本,它们之间互不影响。()14. 程序首先给整型变量x 和y 赋初值 3,5,然后使用传值调用方式调用方法对x 和 y 做乘方并及输出 x 和 y 的乘方值,最后输出x和 y得值。再将此方法给为对象调用加ref修饰查看输出结果差异。参考代码如下:public class Power/ public void MyPower(ref int x, ref int y)public void MyPower(int x, int y)x = 1; y = 2;Console.WriteLine(x=0,y=1, x, y);Console.WriteLine(x*x=0,y*y=1,x*x,y*y);public class Testerpublic static void Main()int x, y;- 7 -x = 3; y = 5;Power mp = new Power();/ mp.MyPower(ref x,ref y); mp.MyPower(x,y); Console.WriteLine(x=0,y=1,x,y); Console.ReadLine();思考:( 1)将响应的注释修改再调试查看结果,分析原因。( 2)将 Main 中 x 和 y 赋初值去掉,结果会怎样?如果 Main 中加 ref,类 Power 的方法中参数前不加 ref 又会有何变化?说明了什么?3)如果不想对x 作无用的初始化,直接作参数传递,怎么实现?15. 编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student 和教师类数据操作类teacher 的基类。参考代码如下:public class personstring ID, name;public void personin(string id,string name)this.ID = id; = name;public void displayin()Console.WriteLine( 编号: 0,ID);Console.WriteLine( 名字: 0, name);public class student : personstring classname;int grads;public student(string classname, int grads)this.classname = classname;this.grads = grads;public void displays()Console.WriteLine( 班级: 0, 成绩: 1,classname,grads);- 8 -public class teacher : personstring title,department;public teacher(string title, string department)this.title = title;this.department = department;public void displayt()Console.WriteLine( 职称: 0, 部门: :1, title, department);public class Testerstatic void Main()student su = new student(0601,69);su.personin(s00001,Tom);su.displayin();su.displays();teacher tc = new teacher(lecture, IM);tc.personin(t0001,LiLi);tc.displayin();tc.displayt();Console.ReadLine();将以上程序尝试改成通过调用基类构造方法的方式来初始化编号和姓名,并总结调用基类构造方法的应用要点。参考代码如下:public class personstring ID, name;public person(string id,string name)this.ID = id; = name;public void displayin()Console.WriteLine( 编号: 0,ID);- 9 -Console.WriteLine( 名字: 0, name);public class student : personstring classname;int grads;public student(string sid,string sname, string classname, int grads):base(sid,sname)this.classname = classname;this.grads = grads;public void displays()Console.WriteLine( 班级: 0, 成绩: 1,classname,grads);public class teacher : personstring title, department;public teacher(string tid, string tname,string title, string department):base(tid,tname)this.title = title;this.department = department;public void displayt()Console.WriteLine( 职称: 0, 部门: :1, title, department);public class Testerstatic void Main()student su = new student(s00001,Tom,0601,69);su.displayin();su.displays();teacher tc = new teacher(t0001,LiLi,lecture, IM);tc.displayin();tc.displayt();Console.ReadLine();- 10 -再尝试将 displayin 方法归并到基类构造方法中去,在主函数中注释掉对本方法的调用,可以实现相同的效果么?16. 编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:定义一个基类圆,至少含有一个数据成员半径;定义基类的派生类球、圆柱、圆锥,都含有求体积函数,可以都在构造函数中实现, 也可以将求体积和输出写在一个函数中, 或者写在两个函数中, 请比较使用。定义主函数,求球、圆柱、圆锥的和体积。class MyCircledouble r;public MyCircle(double r)this.r = r;class MyBall : MyCircledouble volumn;public MyBall(double r): base(r)volumn = Math.PI * r * r * r;Console.WriteLine( 球的体积是:0,volumn);class MyCylinder:MyCircledoublevolumn;public MyCylinder(double r,double h): base(r)public void calvolomn(double r,double h)volumn = Math.PI * r * r * h;public void print()Console.WriteLine( 圆柱体的体积是:0, volumn);- 11 -class MyCone:MyCircledouble h,volumn;public MyCone(double r, double h): base(r)this.h = h;public void calvolumn(double r,double h)volumn = Math.PI * r * r * h / 3;Console.WriteLine( 圆锥体的体积是:0, volumn);public class Testerpublic static void Main()Console.Write( 请输入球半径:);double r=Convert.ToDouble(Console.ReadLine();MyBall mb = new MyBall(r);Console.Write( 请输入圆柱体高度:);double h = Convert.ToDouble(Console.ReadLine();MyCylinder mc = new MyCylinder(r,h);mc.calvolomn(r,h);mc.print();Console.Write( 请输入圆锥体高度:);double h2 = Convert.ToDouble(Console.ReadLine();MyCone mo = new MyCone(r, h2);Console.WriteLine(h2=0,r=1,h2,r);mo.calvolumn(r,h2);Console.ReadLine();17. 多态程序练习:基类 shape类是一个表示形状的抽象类, area( )为求图形面积的函数。请从 shape类派生三角形类 (triangle) 、圆类( circles)、并给出具体的求面积函数,并在主函数中多态地实现调用。using System;namespace Variables- 12 -/public abstract class shape/ public abstract void MyArea();/public class shapepublic virtual void MyArea()Console.WriteLine(no use);public class circle : shapedouble r,carea;public circle(double r)this.r = r;publicoverride void MyArea()carea = Math.PI*r * r;Console.WriteLine( 该图形的面积为0,carea);public class triangle : shapedouble tarea,hemiline,h;public triangle(double hemiline,double h)this.hemiline = hemiline;this.h = h;public override void MyArea()tarea = hemiline * h / 2;Console.WriteLine(hemiline=0,h=1,hemiline,h);Console.WriteLine( 该图形的面积为0, tarea);public class Testerpublic static void Main()- 13 -shape MyShape;double r = Convert.ToDouble(Console.ReadLine();MyShape = new circle(r);MyShape.MyArea();double h = Convert.ToDouble(Console.ReadLine();double hemiline = Convert.ToDouble(Console.ReadLine();MyShape = new triangle(hemiline, h);MyShape.MyArea();Console.ReadLine();思考:(1) 将类 shape 定义为抽象类含有 MyArea 抽象方法再调试程序,查看效果,并回答抽象方法是否可以含 主体?;如果将基类中虚方法关键字virtual 去掉程序会怎样?(2) 将 shape MyShape;和 MyShape = new circle(r); MyShape = new triangle(hemiline, h);三行改成 shape MyShape = new circle(r); shape MyShape = new triangle(hemiline, h); 行不行,为什么?18. 写一个动物基类,具有动物的名称变量,叫虚方法,写出子类猫、狗、牛的叫方法,以多态的方式实现程序;19.程序功能要求, 创建三个结构体, MyCircle,MyCylinder,MyCone 分别表示圆形、 圆柱体和圆锥体, MyCircle 包含一个 int 类型的成员 r 表示半径, MyCylinder 和 MyCone 各自包含一个MyCircel 类型的成员表示圆柱体和圆锥体的底面,成员h和 volumn (都为整型)分别表示圆柱体和圆锥体的高和体积。写出结构体和程序的主方法求圆柱体和圆锥体的体积。struct MyCirclepublic int r;struct MyCylinderpublic MyCircle c;public int h;publicint volumn;struct MyConepublic MyCircle c;public int h;public int volumn;public class Tester- 14 -public static void Main()Console.Write( 请输入底面半径:);MyCircle c = new MyCircle();c.r = int.Parse(Console.ReadLine();Console.Write( 请输入圆柱体高度:);MyCylinder cy = new MyCylinder();cy.h = int.Parse(Console.ReadLine();cy.c = c;Console.Write( 请输入圆锥体高度:);MyCone co = new MyCone();co.h = int.Parse(Console.ReadLine();co.c = c;/计算圆柱体体积double x = Math.PI * cy.c.r * cy.c.r;double y = x * cy.h;cy.volumn = (int)y;/ 计算圆锥体体积double x2 = Math.PI * co.c.r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 全国中学生物理竞赛初赛电磁学难题及答案
- 2025年小初衔接教育试题及答案
- 2025年安全生产泥瓦工安全生产教育试题及答案
- 高中化学竞赛难点突破试题及答案
- 2026年水果种植公司水果运输损失赔偿管理制度
- 2026年水果种植公司水果采摘质量控制管理制度
- 3.1空气与氧气 说课稿-2023-2024学年浙教版八年级下册科学
- Unit 3 Reading I 说课稿 2024-2025学年译林版(2024)七年级英语上册
- 植物分类基础知识说课稿-2025-2026学年中职专业课-天然药物学基础-药剂-医药卫生大类
- 2025年护理质量管理制度题库及答案
- 2024年煤炭工业矿井设计规范
- 2025年杭州市水务集团有限公司招聘笔试参考题库含答案解析
- 二级中医医院评审专家手册
- 我的家乡松原
- 安徽省医疗机构静脉输液管理督导检查表(试行)
- 北师版八年级数学上册 第一章 勾股定理 (压轴专练)(九大题型)
- 3.1细胞膜的结构和功能说课课件-高一上学期生物人教版(2019)必修1
- 人教部编版(五四学制)中国历史第一册复习提纲填空版
- 测定某种食物中的能量说课课件人教版生物七年级下册
- 托班自主活动教案
- 如果历史是一群喵
评论
0/150
提交评论