C#应用开发技术习题.docx

上传人:b****4 文档编号:27265353 上传时间:2023-06-28 格式:DOCX 页数:27 大小:22.30KB
下载 相关 举报
C#应用开发技术习题.docx_第1页
第1页 / 共27页
C#应用开发技术习题.docx_第2页
第2页 / 共27页
C#应用开发技术习题.docx_第3页
第3页 / 共27页
C#应用开发技术习题.docx_第4页
第4页 / 共27页
C#应用开发技术习题.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

C#应用开发技术习题.docx

《C#应用开发技术习题.docx》由会员分享,可在线阅读,更多相关《C#应用开发技术习题.docx(27页珍藏版)》请在冰豆网上搜索。

C#应用开发技术习题.docx

C#应用开发技术习题

C#应用开发技术习题

C#应用开发技术习题

1.用enum定义字节类型的方位常量,打印出某一方位并将此方位值转化为字节类型,字符串类型值。

分析输出结果的原因。

回答以下问题:

Enum的缺省类型是什么?

直接输出myDirection和(byte)myDirection有何区别。

classVariables

{

enumorientation:

byte

{

north=1,

south=2,

east=3,

west=4

}

staticvoidMain(string[]args)

{

bytedirectionByte;

stringdirectionString;

orientationmyDirection=orientation.north;

Console.WriteLine("myDirection={0}",myDirection);

directionByte=(byte)myDirection;

directionString=Convert.ToString(myDirection);

Console.WriteLine("byteequivalent={0}",directionByte);

Console.WriteLine("stringequivalent={0}",directionString);

Console.ReadLine();

}

}

2.建立使用关系运算符和逻辑运算符的程序文件。

Main方法中实例代码如下

staticvoidMain(string[]args)

{

Console.WriteLine("Enteraninteger:

");

intmyInt=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Integerlessthan10?

{0}",myInt<10);

Console.WriteLine("Integerbetween0and5?

{0}",

(0<=myInt)&&(myInt<=5));

Console.WriteLine("BitwiseANDofIntegerand10={0}",myInt&10);

Console.ReadLine();

}

编译运行该程序。

并尝试myInt输入不同范围整数,非10和10时的输出差异。

3.从键盘输入两个数进行比较,并定义一个字符串变量,当数1小于数2时,字符串变量为“lessthan”,当当数1等小于数2时字符串变量为“equalto”,当数1大于数2时字符串变量为“greaterthan”。

staticvoidMain(string[]args)

{

stringcomparison;

Console.WriteLine("Enteranumber:

");

doublevar1=Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enteranothernumber:

");

doublevar2=Convert.ToDouble(Console.ReadLine());

if(var1

comparison="lessthan";

else

{

if(var1==var2)

comparison="equalto";

else

comparison="greaterthan";

}

Console.WriteLine("Thefirstnumberis{0}thesecondnumber.",

comparison);

Console.ReadLine();

}

4.定义三个常量字符串“karli”,"angelina","ploppy",并从键盘输入一个名字,当名字与“karli”相同时输出我们的名字相同,当和"angelina"名字相同时输出你的名字太性感了,当和"ploppy"相同时输出这名字真傻。

staticvoidMain(string[]args)

{

conststringmyName="karli";

conststringsexyName="angelina";

conststringsillyName="ploppy";

stringname;

Console.WriteLine("Whatisyourname?

");

name=Console.ReadLine();

switch(name.ToLower())

{

casemyName:

Console.WriteLine("Youhavethesamenameasme!

");

break;

casesexyName:

Console.WriteLine("My,whatasexynameyouhave!

");

break;

casesillyName:

Console.WriteLine("That'saverysillyname.");

break;

}

Console.WriteLine("Hello{0}!

",name);

Console.ReadLine();

}

5.for循环语句练习

(1)程序功能要求:

按5度的增量打印出一个从摄氏温度到华氏温度的转换表。

staticvoidMain(string[]args)

{

doubleFa,Cel;

Cel=0;

for(Cel=0;Cel<100;Cel+=5)

{

Fa=Cel*9/5;

Console.WriteLine(Fa);

Console.ReadLine();

}

(2)自行改造以上程序。

6.while循环语句练习

(1)程序功能要求:

运行程序后从键盘输入数字1/2/3后,可显示抽奖得到的奖品:

恭喜你得了一辆汽车;不错啊,一台笔记本电脑;没白来,一个MP3;如果输入其它数字或字符显示“没有奖品给你!

”。

示例代码如下:

intchoice;

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中的“==”改为一个“=”,看效果如何?

并分析错误。

7.do…while循环语句练习

程序功能要求:

输入你现有的存款和当前的年利率及你期望将来得到的存款,计算出存款多少年后才可以变成你期望的存款额。

注意,若为一年输出year为year,若为多年输出year为years。

参考代码如下:

staticvoidMain(string[]args)

{

doublebalance,interestRate,targetBalance;

Console.WriteLine("Whatisyourcurrentbalance?

");

balance=Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Whatisyourcurrentannualinterestrate(in%)?

");

interestRate=1+Convert.ToDouble(Console.ReadLine())/100.0;

Console.WriteLine("Whatbalancewouldyouliketohave?

");

targetBalance=Convert.ToDouble(Console.ReadLine());

inttotalYears=0;

do

{

balance*=interestRate;

++totalYears;

}

while(balance

Console.WriteLine("In{0}year{1}you'llhaveabalanceof{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.定义一个圆类,计算圆的面积和周长

publicclasscircle

{

publicstaticvoidMain()

{

doubleradium,delimeter,square;

constdoublepai=3.1415926;

radium=Convert.ToInt32(Console.ReadLine());

delimeter=2*pai*radium;

square=pai*pai*radium;

Console.WriteLine("delimeter={0},square={1}",delimeter,square);

Console.ReadLine();

}

}

或者:

publicclasscircle

{

doubledelimeter,square;

constdoublepai=3.1415926;

publicvoidcalculate(doublerad)

{

delimeter=2*pai*rad;

square=pai*pai*rad;

Console.WriteLine("delimeter={0},square={1}",delimeter,square);

}

publicstaticvoidMain()

{

doubleradium;

circlecir=newcircle();

radium=Convert.ToInt32(Console.ReadLine());

cir.calculate(radium);

Console.ReadLine();

}

}

请比较以上两个程序,看起来后一个程序把问题复杂化了,是不是不如第一个程序好,它从设计思想上有什么优势么?

12.程序要求如下:

其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。

同时编写主函数使用这个类,实现对学生数据的赋值和输出。

要求:

使用成员函数实现对数据的输出;使用构造函数实现对数据的输入。

参考代码如下:

publicclassstudents

{

stringid,name;

intage;

publicstudents(stringid,stringname,intage)

{

this.id=id;

this.name=name;

this.age=age;

}

publicvoidDisplay()

{

Console.WriteLine("id={0},name={1},age={2}",id,name,age);

}

publicstaticvoidMain()

{

//stringid,name;

//intage;

studentsstu=newstudents("0001","zhangsan",16);

stu.Display();

Console.ReadLine();

}

}

以上程序使用了构造方法,请回答关键字this有何作用,你能将成员函数Display修改成别的代码也实现响应的功能么?

13.编写帐户类,对每一账号赋值帐户并设置初始化存款为0.00元,设计一变量统计账号生成的数目。

publicclassBankAccount

{

staticinttotalAccountNumber=0;

stringBankAccountId;

doubleinitialDepositAmount=0.00;

publicBankAccount(stringmyId)

{

this.BankAccountId=myId;

this.initialDepositAmount=0.00;

totalAccountNumber++;

}

publicvoiddisplayid()

{

Console.WriteLine("mbaid={0},initialDepositAmount={1}",this.BankAccountId,this.initialDepositAmount);

}

publicstaticvoiddisplay()

{

Console.WriteLine("totalAccountNumber={0}",totalAccountNumber);

}

}

publicclassTester

{

publicstaticvoidMain()

{

BankAccountmba=newBankAccount("37000001");

BankAccountmba2=newBankAccount("3700002");

BankAccountmba3=newBankAccount("");

BankAccountmba4=newBankAccount("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修饰查看输出结果差异。

参考代码如下:

publicclassPower

{

//publicvoidMyPower(refintx,refinty)

publicvoidMyPower(intx,inty)

{

x=1;y=2;

Console.WriteLine("x={0},y={1}",x,y);

Console.WriteLine("x*x={0},y*y={1}",x*x,y*y);

}

}

publicclassTester

{

publicstaticvoidMain()

{

intx,y;

x=3;y=5;

Powermp=newPower();

//mp.MyPower(refx,refy);

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的基类。

参考代码如下:

publicclassperson

{

stringID,name;

publicvoidpersonin(stringid,stringname)

{

this.ID=id;

this.name=name;

}

publicvoiddisplayin()

{

Console.WriteLine("编号:

{0}",ID);

Console.WriteLine("名字:

{0}",name);

}

}

publicclassstudent:

person

{

stringclassname;

intgrads;

publicstudent(stringclassname,intgrads)

{

this.classname=classname;

this.grads=grads;

}

publicvoiddisplays()

{

Console.WriteLine("班级:

{0},成绩:

{1}",classname,grads);

}

}

publicclassteacher:

person

{

stringtitle,department;

publicteacher(stringtitle,stringdepartment)

{

this.title=title;

this.department=department;

}

publicvoiddisplayt()

{

Console.WriteLine("职称:

{0},部门:

:

{1}",title,department);

}

}

publicclassTester

{

staticvoidMain()

{

studentsu=newstudent("0601",69);

su.personin("s00001","Tom");

su.displayin();

su.displays();

teachertc=newteacher("lecture","IM");

tc.personin("t0001","LiLi");

tc.displayin();

tc.displayt();

Console.ReadLine();

}

}

将以上程序尝试改成通过调用基类构造方法的方式来初始化编号和姓名,并总结调用基类构造方法的应用要点。

参考代码如下:

publicclassperson

{

stringID,name;

publicperson(stringid,stringname)

{

this.ID=id;

this.name=name;

}

publicvoiddisplayin()

{

Console.WriteLine("编号:

{0}",ID);

Console.WriteLine("名字:

{0}",name);

}

}

publicclassstudent:

person

{

stringclassname;

intgrads;

publicstudent(stringsid,stringsname,stringclassname,intgrads):

base(sid,sname)

{

this.classname=classname;

this.grads=grads;

}

publicvoiddisplays()

{

Console.WriteLine("班级:

{0},成绩:

{1}",classname,grads);

}

}

publicclassteacher:

person

{

stringtitle,department;

publicteacher(stringtid,stringtname,stringtitle,stringdepartment):

base(tid,tname)

{

this.title=title;

this.department=department;

}

publicvoiddisplayt()

{

Console.WriteLine("职称:

{0},部门:

:

{1}",title,department);

}

}

publicclassTester

{

staticvoidMain()

{

studentsu=newstudent("s00001","Tom","0601",69);

su.displayin();

su.displays();

teachertc=newteacher("t0001","LiLi","lecture","IM");

tc.displayin();

tc.displayt(

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1