C#入门经典解题答案.docx
- 文档编号:9728826
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:134
- 大小:56.84KB
C#入门经典解题答案.docx
《C#入门经典解题答案.docx》由会员分享,可在线阅读,更多相关《C#入门经典解题答案.docx(134页珍藏版)》请在冰豆网上搜索。
C#入门经典解题答案
问题解答
Chapter1:
IntroducingC#
Noexercises.
Chapter2:
WritingaC#Program
Noexercises.
Chapter3:
VariablesandExpressions
Exercise1
Q.Inthefollowingcode,howwouldyourefertothenamegreatfromcodeinthenamespacefabulous?
namespacefabulous
{
//codeinfabulousnamespace
}
namespacesuper
{
namespacesmashing
{
//greatnamedefined
}
}
A.
super.smashing.great
Exercise2
Q.Whichofthefollowingisnotalegalvariablename?
a.myVariableIsGood
b.99Flake
c._floor
dtime2GetJiggyWidIt
e
A.
b.Becauseitstartswithanumber,and,
eBecauseitcontainsafullstop.
Exercise3
Q.Isthestring"supercalifragilisticexpialidocious"toobigtofitinastringvariable?
Why?
A.No,thereisnotheoreticallimittothesizeofastringthatmaybecontainedinastringvariable.
Exercise4
Q.Byconsideringoperatorprecedence,listthestepsinvolvedinthecomputationofthefollowingexpression:
resultVar+=var1*var2+var3%var4/var5;
A.The*and/operatorshavethehighestprecedencehere,followedby+,%,andfinally+=.Theprecedenceintheexercisecanbeillustratedusingparenthesesasfollows:
resultVar+=(((var1*var2)+var3)%(var4/var5));
Exercise5
Q.Writeaconsoleapplicationthatobtainsfourintvaluesfromtheuseranddisplaystheirproduct.
A.
staticvoidMain(string[]args)
{
intfirstNumber,secondNumber,thirdNumber,fourthNumber;
Console.WriteLine("Givemeanumber:
");
firstNumber=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Givemeanothernumber:
");
secondNumber=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Givemeanothernumber:
");
thirdNumber=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Givemeanothernumber:
");
fourthNumber=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Theproductof{0},{1},{2},and{3}is{4}.",
firstNumber,secondNumber,thirdNumber,fourthNumber,
firstNumber*secondNumber*thirdNumber*fourthNumber);
}
NotethatConvert.ToInt32()isusedhere,whichisn’tcoveredinthechapter.
Chapter4:
FlowControl
Exercise1
Q.Ifyouhavetwointegersstoredinvariablesvar1andvar2,whatBooleantestcanyouperformtoseeifoneortheother(butnotboth)isgreaterthan10?
A.(var1>10)^(var2>10)
Exercise2
Q.WriteanapplicationthatincludesthelogicfromExercise1,obtainstwonumbersfromtheuser,anddisplaysthem,butrejectsanyinputwherebothnumbersaregreaterthan10andasksfortwonewnumbers.
A.
staticvoidMain(string[]args)
{
boolnumbersOK=false;
doublevar1,var2;
var1=0;
var2=0;
while(!
numbersOK)
{
Console.WriteLine("Givemeanumber:
");
var1=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Givemeanothernumber:
");
var2=Convert.ToDouble(Console.ReadLine());
if((var1>10)^(var2>10))
{
numbersOK=true;
}
else
{
if((var1<=10)&&(var2<=10))
{
numbersOK=true;
}
else
{
Console.WriteLine("Onlyonenumbermaybegreaterthan10.");
}
}
}
Console.WriteLine("Youentered{0}and{1}.",var1,var2);
}
Thiscanbeperformedbetterusingdifferentlogic,forexample:
staticvoidMain(string[]args)
{
boolnumbersOK=false;
doublevar1,var2;
var1=0;
var2=0;
while(!
numbersOK)
{
Console.WriteLine("Givemeanumber:
");
var1=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Givemeanothernumber:
");
var2=Convert.ToDouble(Console.ReadLine());
if((var1>10)&&(var2>10))
{
Console.WriteLine("Onlyonenumbermaybegreaterthan10.");
}
else
{
numbersOK=true;
}
}
Console.WriteLine("Youentered{0}and{1}.",var1,var2);
}
Exercise3
Q.Whatiswrongwiththefollowingcode?
inti;
for(i=1;i<=10;i++)
{
if((i%2)=0)
continue;
Console.WriteLine(i);
}
A.Thecodeshouldread:
inti;
for(i=1;i<=10;i++)
{
if((i%2)==0)
continue;
Console.WriteLine(i);
}
Usingthe=assignmentoperatorinsteadoftheBoolean==operatorisacommonmistake.
Exercise4
Q.ModifytheMandelbrotsetapplicationtoreQuestimagelimitsfromtheuseranddisplaythechosensectionoftheimage.Thecurrentcodeoutputsasmanycharactersaswillfitonasinglelineofaconsoleapplication;considermakingeveryimagechosenfitinthesameamountofspacetomaximizetheviewablearea.
A.
staticvoidMain(string[]args)
{
doublerealCoord,imagCoord;
doublerealMax=1.77;
doublerealMin=-0.6;
doubleimagMax=-1.2;
doubleimagMin=1.2;
doublerealStep;
doubleimagStep;
doublerealTemp,imagTemp,realTemp2,arg;
intiterations;
while(true)
{
realStep=(realMax-realMin)/79;
imagStep=(imagMax-imagMin)/48;
for(imagCoord=imagMin;imagCoord>=imagMax;
imagCoord+=imagStep)
{
for(realCoord=realMin;realCoord<=realMax;
realCoord+=realStep)
{
iterations=0;
realTemp=realCoord;
imagTemp=imagCoord;
arg=(realCoord*realCoord)+(imagCoord*imagCoord);
while((arg<4)&&(iterations<40))
{
realTemp2=(realTemp*realTemp)-(imagTemp*imagTemp)
-realCoord;
imagTemp=(2*realTemp*imagTemp)-imagCoord;
realTemp=realTemp2;
arg=(realTemp*realTemp)+(imagTemp*imagTemp);
iterations+=1;
}
switch(iterations%4)
{
case0:
Console.Write(".");
break;
case1:
Console.Write("o");
break;
case2:
Console.Write("O");
break;
case3:
Console.Write("@");
break;
}
}
Console.Write("\n");
}
Console.WriteLine("Currentlimits:
");
Console.WriteLine("realCoord:
from{0}to{1}",realMin,realMax);
Console.WriteLine("imagCoord:
from{0}to{1}",imagMin,imagMax);
Console.WriteLine("Enternewlimits:
");
Console.WriteLine("realCoord:
from:
");
realMin=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("realCoord:
to:
");
realMax=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("imagCoord:
from:
");
imagMin=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("imagCoord:
to:
");
imagMax=Convert.ToDouble(Console.ReadLine());
}
}
Chapter5:
MoreAboutVariables
Exercise1
Q.Whichofthefollowingconversionscan’tbeperformedimplicitly:
a.inttoshort
b.shorttoint
c.booltostring
d.bytetofloat
A.Conversionsaandccan’tbeperformedimplicitly.
Exercise2
Q.Givethecodeforacolorenumerationbasedontheshorttypecontainingthecolorsoftherainbowplusblackandwhite.Canthisenumerationbebasedonthebytetype?
A.
enumcolor:
short
{
Red,Orange,Yellow,Green,Blue,Indigo,Violet,Black,White
}
Yes,becausethebytetypecanholdnumbersbetween0and255,sobyte-basedenumerationscanhold256entrieswithindividualvalues,ormoreifduplicatevaluesareusedforentries.
Exercise3
Q.ModifytheMandelbrotsetgeneratorexamplefromthelastchaptertousethefollowingstructforcomplexnumbers:
structimagNum
{
publicdoublereal,imag;
}
A.
staticvoidMain(string[]args)
{
imagNumcoord,temp;
doublerealTemp2,arg;
intiterations;
for(coord.imag=1.2;coord.imag>=-1.2;coord.imag-=0.05)
{
for(coord.real=-0.6;coord.real<=1.77;coord.real+=0.03)
{
iterations=0;
temp.real=coord.real;
temp.imag=coord.imag;
arg=(coord.real*coord.real)+(coord.imag*coord.imag);
while((arg<4)&&(iterations<40))
{
realTemp2=(temp.real*temp.real)-(temp.imag*temp.imag)
-coord.real;
temp.imag=(2*temp.real*temp.imag)-coord.imag;
temp.real=realTemp2;
arg=(temp.real*temp.real)+(temp.imag*temp.imag);
iterations+=1;
}
switch(iterations%4)
{
case0:
Console.Write(".");
break;
case1:
Console.Write("o");
break;
case2:
Console.Write("O");
break;
case3:
Console.Write("@");
break;
}
}
Console.Write("\n");
}
}
Exercise4
Q.Willthefollowingcodecompile?
Why?
string[]blab=newstring[5]
string[5]=5thstring.
A.No,forthefollowingreasons:
*Endofstatementsemicolonsaremissing.
*2ndlineattemptstoaccessanon-existent6thelementofblab.
*2ndlineattemptstoassignastringthatisn’tenclosedindoublequotes.
Exercise5
Q.Writeaconsoleapplicationthatacceptsastringfromtheuserandoutputsastringwiththecharactersinreverseorder.
A.
staticvoidMain(string[]args)
{
Console.WriteLine("Enterastring:
");
stringmyString=Console.ReadLine();
stringreversedString="";
for(intindex=myString.Length-1;index>=0;index--)
{
reversedString+=myString[index];
}
Console.WriteLine("Reversed:
{0}",reversedString);
}
Exercise6
Q.Writeaconsoleapplicationthatacceptsastringandreplacesalloccurrencesofthestringnowithyes.
A.
staticvoidMain(string[]args)
{
Console.WriteLine("Enterastring:
");
stringmyString=Console.ReadLine();
myString=myString.Replace("no","yes");
Console.WriteLine("Replaced\"no\"with\"yes\":
{0}",myString);
}
Exercise7
Q.WriteaconsoleapplicationthatplacesdoubleQuotesaroundeachwordinastring.
A.
staticvoidMain(string[]args)
{
Console.WriteLine("Enterastring:
");
stringmyString=Console.ReadLine();
myString="\""+myString.Replace("","\"\"")+"\"";
Console.WriteLine("Addeddoublequotesareoundwords:
{0}",myString);
}
OrusingString.Split():
staticvoidMain(string[]args)
{
Console.WriteLine("Enterastring:
");
stringmyString=Console.ReadLine();
string[]myWords=myString.Split('');
Console.WriteLine("Addingdoublequotesareoundwords:
");
foreach(stringmyWordinmyWords)
{
Console.Write("\"{0}\"",myWord);
}
}
Chapter6:
Functions
Exercise1
Q.Thefollowi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 入门 经典 解题 答案