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();
Console.ReadLine();