C#金雪云课后题参考答案Word文件下载.docx
《C#金雪云课后题参考答案Word文件下载.docx》由会员分享,可在线阅读,更多相关《C#金雪云课后题参考答案Word文件下载.docx(62页珍藏版)》请在冰豆网上搜索。
else
Other++;
index++;
}
字符串分析结果为:
"
字母有{0}个"
numLetters);
数字有{0}个"
numDigits);
其他支符有{0}个"
Other);
}
(3)
namespace上机练习3
classRectangle
privatedoublex;
privatedoubley;
publicRectangle(doublex,doubley)
this.x=x;
this.y=y;
publicdoubleGetArea()
returnx*y;
publicdoubleGetZhouChang()
return(x+y)*2;
doublea=1.5;
doubleb=2.3;
长方形的周长是:
+(a+b)*2);
长方形的面积是:
+a*b);
Rectanglert=newRectangle(1.5,2.3);
+rt.GetArea());
+rt.GetZhouChang());
(4)
namespaceConsoleApplication1
stringstr=Console.ReadLine();
char[]ch=str.ToCharArray();
//字符串转换为字符数组
//输出转换结果
foreach(charcinch)
{
Console.WriteLine("
{0}"
c);
}
//实现反转
char[]chtemp=str.ToCharArray();
intlongs=ch.GetLength(0);
for(inti=0;
i<
=longs-1;
i++)
chtemp[i]=ch[longs-i-1];
}
////使用修改后的字符数组构造新字符串
stringstr2=newstring(chtemp);
Console.WriteLine(str2);
Console.ReadLine();
(5)程序为:
namespace上机练习5
classEmployee
privateintid;
publicintId
get{returnid;
set{id=value;
privatestringname;
publicstringName
get{returnname;
set{name=value;
privateboolmarry;
publicboolMarry
get{returnmarry;
set{marry=value;
privatedecimalsalary;
publicdecimalSalary
get{returnsalary;
set{salary=value;
publicEmployee(intid,stringname,boolmarry,decimalsalary)
this.id=id;
this.name=name;
this.marry=marry;
this.salary=salary;
intid;
stringname;
boolmarry;
decimalsalary;
请输入员工的工号"
id=int.Parse(Console.ReadLine());
请输入员工姓名"
name=Console.ReadLine();
请输入员工婚否?
(已婚请输入字母“y”;
未婚请输入字母“n”)"
if(Console.ReadLine().ToLower().Equals("
y"
))
marry=true;
marry=false;
请输入员工工资"
salary=Convert.ToDecimal(Console.ReadLine());
Object[]array=newObject[1];
array[0]=newEmployee(id,name,marry,salary);
foreach(Employeeiteminarray)
该员工的工号为:
+item.Id);
该员工的姓名为:
+item.Name);
if(item.Marry==true)
该员工已婚"
else
该员工未婚"
该员工的工资为:
+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)在需要实例化出一个引用类型的对象时使用。
第三章
(2)六行杨辉三角
(3)五行杨辉三角
classTest
publicstaticintmax(inta,intb,intc,intd)
inttempmax=a;
if(tempmax<
b)
tempmax=b;
c)
tempmax=c;
d)
tempmax=d;
returntempmax;
publicstaticintmin(inta,intb,intc,intd)
inttempmin=a;
if(tempmin>
tempmin=b;
tempmin=c;
tempmin=d;
returntempmin;
publicstaticvoidMain()
Pleaseinputfournumber:
intx1=Convert.ToInt32(Console.ReadLine());
intx2=Convert.ToInt32(Console.ReadLine());
intx3=Convert.ToInt32(Console.ReadLine());
intx4=Convert.ToInt32(Console.ReadLine());
TheMaxNumberis:
max(x1,x2,x3,x4));
TheMinNumberis:
min(x1,x2,x3,x4));
(5)
usingSystem;
publicstaticintaddfor()
intsum=0;
for(inti=0;
=50;
i++)
sum+=i;
returnsum;
publicstaticintadddo()
inti=0;
do
i++;
while(i<
=50);
publicstaticintaddwhile()
=50)
TheSumforis:
addwhile());
TheSumdois:
addwhile());
TheSumwhileis:
习题
(1)B
(2)ACD(3)D(4)D(5)A
(1)顺序、分支、循环
(2)break;
reuturn;
goto;
continue
(3)3
(4)集合中元素的类型
(5)在C#语句和表达式的处理过程中激发了某个异常的条件,使得操作无法正常结束,引发异常、抛出异常
(1)判断表达式一定要合法
(2)计算表达式的值;
与分支进行匹配;
执行分支语句;
结束。
(3)提高程序的可读性和健壮性
Try块的代码是程序中可能出现错误的操作部分。
Catch块的代码是用来处理各种错误的部分(可以有多个)。
必须正确排列捕获异常的catch子句,范围小的Exception放在前面的catch。
即如果Exception之间存在继承关系,就应把子类的Exception放在前面的catch子句中。
Finally块的代码用来清理资源或执行要在try块末尾执行的其他操作(可以省略)。
且无论是否产生异常,Finally块都会执行
第四章
(1)…
(2)
classMainClass
publicstaticvoidMain(string[]args)
int[,,]Score={{{1,2},{3,4}},{{5,6},{7,8}}};
for(inti=0;
2;
for(intj=0;
j<
j++)
for(intk=0;
k<
k++)
sum+=Score[i,j,k];
Thesumis:
sum);
int[,]arr=newint[3,3];
inti,j,t,m=0,k=0,flag=0;
for(i=0;
3;
i++)
for(j=0;
j++)
arr[i,j]=Convert.ToInt32(Console.ReadLine());
/*求出每一行的最大值,并判断它是否为所在列的最小值*/
for(i=0;
i<
3;
i++)
t=arr[i,0];
for(j=0;
j<
j++)
if(t<
=arr[i,j])
t=arr[i,j];
m=i;
k=j;
if(arr[m,k]>
arr[i,k])
flag=1;
/*鞍点存在,查找是否有多个鞍点*/
if(flag==0)
/*找出鞍点个数并打印出来*/
if(arr[m,j]==arr[m,k])
//printf("
thesaddlepointa[%d][%d]=%d\n"
m,j,a[m][j]);
Thesaddlepointa[{0}][{1}]={2}\n"
m,j,arr[m,j]);
therearenosaddlepoint"
3;
arr[i,j]=Convert.ToInt32(Console.ReadLine());
if(i==j||i+j==2)
sum+=arr[i,j];
对角线元素之和为:
sum);
(5)
usingSystem.Collections;
int[]a=newint[100];
intb=0,n=0,i=0,j=0,k=0;
n=Convert.ToInt32(Console.ReadLine());
b=n;
while(b>
0)
a[i]=b%2;
b/=2;
j++;
Theresultis:
for(k=j-1;
k>
=0;
k--)
a[k]);
(6)
classfriend
stringname="
;
stringtelephone="
stringgroup="
get
returnname;
set
name=value;
publicstringTelephone
returntelephone;
telephone=value;
publicstringGroup
returngroup;
group=value;
publicfriend(stringn,stringt,stringg)
name=n;
telephone=t;
group=g;
publicfriend(stringn)
telephone="
group="
publicvoidDisplay()
-----------------------------"
姓名:
{0}\n"
this.Name);
电话:
this.telephone);
组:
this.group);
classtelephonebook
privateArrayListtele;
publictelephonebook()
this.tele=newArrayList();
publicvoidAdd(friendf)
this.tele.Add(f);
publicvoidDelete(stringna)
friendtemp=null;
foreach(friendfinthis.tele)
if(((friend)f).Name==na)
temp=f;
if(temp!
=null)
this.tele.Remove(temp);
publicvoidEdit(stringna)
stringname,tele,group;
请输入修改后的名称:
请输入修改后的电话:
tele=Console.ReadLine();
请输入修改后的组:
group=Console.ReadLine();
this.Delete(na);
friendtemp=newfriend(name,tele,group);
this.tele.Add(temp);
publicfriendSearchByName(stringna)
foreach(friendfinthis.tele