tempmax=d;
returntempmax;
}
publicstaticintmin(inta,intb,intc,intd)
{
inttempmin=a;
if(tempmin>b)
tempmin=b;
if(tempmin>c)
tempmin=c;
if(tempmin>d)
tempmin=d;
returntempmin;
}
publicstaticvoidMain()
{
Console.WriteLine("Pleaseinputfournumber:
");
intx1=Convert.ToInt32(Console.ReadLine());
intx2=Convert.ToInt32(Console.ReadLine());
intx3=Convert.ToInt32(Console.ReadLine());
intx4=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("TheMaxNumberis:
{0}",max(x1,x2,x3,x4));
Console.WriteLine("TheMinNumberis:
{0}",min(x1,x2,x3,x4));
Console.ReadLine();
}
}
(5)usingSystem;
classTest
{
publicstaticintaddfor()
{
intsum=0;
for(inti=0;i<=50;i++)
sum+=i;
returnsum;
}
publicstaticintadddo()
{
intsum=0;
inti=0;
do
{
sum+=i;
i++;
}
while(i<=50);
returnsum;
}
publicstaticintaddwhile()
{
intsum=0;
inti=0;
while(i<=50)
{
sum+=i;
i++;
}
returnsum;
}
publicstaticvoidMain()
{
Console.WriteLine("TheSumforis:
{0}",addwhile());
Console.WriteLine("TheSumdois:
{0}",addwhile());
Console.WriteLine("TheSumwhileis:
{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)
usingSystem;
classMainClass
{
publicstaticvoidMain(string[]args)
{
intsum=0;
int[,,]Score={{{1,2},{3,4}},{{5,6},{7,8}}};
for(inti=0;i<2;i++)
{
for(intj=0;j<2;j++)
{
for(intk=0;k<2;k++)
{
sum+=Score[i,j,k];
}
}
}
Console.WriteLine("Thesumis:
{0}",sum);
Console.ReadLine();
}
}
(3)
usingSystem;
classMainClass
{
publicstaticvoidMain(string[]args)
{
int[,]arr=newint[3,3];
inti,j,t,m=0,k=0,flag=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
arr[i,j]=Convert.ToInt32(Console.ReadLine());
}
/*求出每一行的最大值,并判断它是否为所在列的最小值*/
for(i=0;i<3;i++)
{
t=arr[i,0];
for(j=0;j<3;j++)
{
if(t<=arr[i,j])
{
t=arr[i,j];
m=i;
k=j;
}
}
for(i=0;i<3;i++)
{
if(arr[m,k]>arr[i,k])
flag=1;
}
/*鞍点存在,查找是否有多个鞍点*/
if(flag==0)
{
/*找出鞍点个数并打印出来*/
for(j=0;j<3;j++)
if(arr[m,j]==arr[m,k])
//printf("thesaddlepointa[%d][%d]=%d\n",m,j,a[m][j]);
Console.WriteLine("Thesaddlepointa[{0}][{1}]={2}\n",m,j,arr[m,j]);
}
else
Console.WriteLine("therearenosaddlepoint");
}
Console.ReadLine();
}
}
(4)
usingSystem;
classMainClass
{
publicstaticvoidMain(string[]args)
{
int[,]arr=newint[3,3];
intsum=0;
for(inti=0;i<3;i++)
{
for(intj=0;j<3;j++)
{
arr[i,j]=Convert.ToInt32(Console.ReadLine());
if(i==j||i+j==2)
{
sum+=arr[i,j];
}
}
}
Console.WriteLine("对角线元素之和为:
{0}",sum);
Console.ReadLine();
}
}
(5)
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
classMainClass
{
publicstaticvoidMain(string[]args)
{
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;
i++;
j++;
}
Console.WriteLine("Theresultis:
");
for(k=j-1;k>=0;k--)
Console.WriteLine("{0}",a[k]);
Console.ReadLine();
}
}
(6)
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
classfriend
{
stringname="";
stringtelephone="";
stringgroup="";
publicstringName
{
get
{
returnname;
}
set
{
name=value;
}
}
publicstringTelephone
{
get
{
returntelephone;
}
set
{
telephone=value;
}
}
publicstringGroup
{
get
{
returngroup;
}
set
{
group=value;
}
}
publicfriend(stringn,stringt,stringg)
{
name=n;
telephone=t;
group=g;
}
publicfriend(stringn)
{
name=n;
telephone="";
group="";
}
publicvoidDisplay()
{
Console.WriteLine("-----------------------------");
Console.WriteLine("姓名:
{0}\n",this.Name);
Console.WriteLine("电话:
{0}\n",this.telephone);
Console.WriteLine("组:
{0}\n",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;
Console.WriteLine("请输入修改后的名称:
");
name=Console.ReadLine();
Console.WriteLine("请输入修改后的电话:
");
tele=Console.ReadLine();
Console.WriteLine("请输入修改后的组:
");
group=Console.ReadLine();
this.Delete(na);
friendtemp=newfriend(name,tele,group);
this.tele.Add(temp);
}
publicfriendSearchByName(stringna)
{
foreach(friendfinthis.tele)
{
if(f.Name==na)
returnf;
}
returnnull;
}
publicfriendSearchByTele(stringtel)
{
foreach(friendfinthis.tele)
{
if(f.Telephone==tel)
returnf;
}
returnnull;
}
publicvoiddisplay()
{
foreach(friendfinthis.tele)
{
f.Display();
}
}
}
classMainClass
{
publicstaticvoidMain(string[]args)
{
telephonebooktb=newtelephonebook();
friendzangsan=newfriend("zs","123","jiaren");
zangsan.Display();
tb.Add(zangsan);
friendtemp=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)C
2、填空题
(1)Sort()
(2)抛出异常、集合已修改;可能无法执行枚举操作。
(3)获取头部元素数据
(4)Peek方法仅获取栈顶元素数据,Pop方法删除栈顶元素
(5)实现System.IEnumerable接口或实现集合模式
(6)名值对
(7)SortedList中的Key数组排好序的
第五章
上机练习
(1)……
(2)
classstudent
{
publicstringname;
privateintage;
privateintheigh;
privatestringgrade;
publicstudent()
{
this.name="";
this.age=18;
this.heigh=180;
this.grade="0604";
}
publicstudent(stringna,inta,inth,stringg)
{
this.grade=g;
this.name=na;
this.heigh=h;
this.age=a;
}
~student()
{
}
}
(3)
usingSystem;
usingSystem.Collections.Generic;
classEmployee
{
privatestringname;
privatestringID;
publicstaticfloatTotalSalary;
//定义实例构造函数,用来初始化实例中成员
publicEmployee()
{
this.name="";
this.price="0000";
}
publicEmployee(stringn,stringi)
{
this.name=n;
this.ID=i;
}
//定义静态构造函数
staticEmployee()
{
TotalSalary=0;
}
//定义析构函数
~Employee()
{
}
publicvoidprintName()
{
Console.WriteLine(this.name);
}
}
(4)
usingSystem;
usingSystem.Collections.Generic;
classEmployee
{
privatestringname;
privatestringID;
publicstaticfloatTotalSalary;
privatefloatSalary;
//定义实例构造函数,用来初始化实例中成员
publicEmployee()
{
this.name="";
this.ID="0000";
this.Salary=0;
}
publicEmployee(stringn,stringi,floats)
{
this.name=n;
this.ID=i;
this.Salary=s;
TotalSalary+=this.Salary;
}
//定义静态构造函数
staticEmployee()
{
TotalSalary=0;
}
//定义析构函数
~Employee()
{
}
publicvoidprintName()
{
Console.WriteLine(this.name);
}
publicstaticvoidDisplayTotalSalary()
{
Console.WriteLine("总薪水为:
{0}",TotalSalary);
}
}
(5)
usingSystem;
usingSystem.Collections.Generic;
classAccount
{
privatestringID;
privatestringPSW;
privatestringName;
privatefloatBalance;
publicstaticfloatTotalBalance;
//定义实例构造函数,用来初始化实例中成员
publicAccount(stringi,stringp,stringn,floatb)
{
this.Balance=b;
this.ID=i;
this.Name=n;
this.PSW=p;
TotalBalance+=this.Balance;
}
s