C#入门经典解题答案Word文件下载.docx
- 文档编号:22931420
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:134
- 大小:56.84KB
C#入门经典解题答案Word文件下载.docx
《C#入门经典解题答案Word文件下载.docx》由会员分享,可在线阅读,更多相关《C#入门经典解题答案Word文件下载.docx(134页珍藏版)》请在冰豆网上搜索。
a.myVariableIsGood
b.99Flake
c._floor
dtime2GetJiggyWidIt
e
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.
staticvoidMain(string[]args)
intfirstNumber,secondNumber,thirdNumber,fourthNumber;
Console.WriteLine("
Givemeanumber:
"
);
firstNumber=Convert.ToInt32(Console.ReadLine());
Givemeanothernumber:
secondNumber=Convert.ToInt32(Console.ReadLine());
thirdNumber=Convert.ToInt32(Console.ReadLine());
fourthNumber=Convert.ToInt32(Console.ReadLine());
Theproductof{0},{1},{2},and{3}is{4}."
firstNumber,secondNumber,thirdNumber,fourthNumber,
firstNumber*secondNumber*thirdNumber*fourthNumber);
NotethatConvert.ToInt32()isusedhere,whichisn’tcoveredinthechapter.
Chapter4:
FlowControl
Q.Ifyouhavetwointegersstoredinvariablesvar1andvar2,whatBooleantestcanyouperformtoseeifoneortheother(butnotboth)isgreaterthan10?
A.(var1>
10)^(var2>
10)
Q.WriteanapplicationthatincludesthelogicfromExercise1,obtainstwonumbersfromtheuser,anddisplaysthem,butrejectsanyinputwherebothnumbersaregreaterthan10andasksfortwonewnumbers.
boolnumbersOK=false;
doublevar1,var2;
var1=0;
var2=0;
while(!
numbersOK)
var1=Convert.ToDouble(Console.ReadLine());
var2=Convert.ToDouble(Console.ReadLine());
if((var1>
10))
numbersOK=true;
else
if((var1<
=10)&
&
(var2<
=10))
Onlyonenumbermaybegreaterthan10."
Youentered{0}and{1}."
var1,var2);
Thiscanbeperformedbetterusingdifferentlogic,forexample:
10)&
(var2>
Q.Whatiswrongwiththefollowingcode?
inti;
for(i=1;
i<
=10;
i++)
if((i%2)=0)
continue;
Console.WriteLine(i);
A.Thecodeshouldread:
if((i%2)==0)
Usingthe=assignmentoperatorinsteadoftheBoolean==operatorisacommonmistake.
Q.ModifytheMandelbrotsetapplicationtoreQuestimagelimitsfromtheuseranddisplaythechosensectionoftheimage.Thecurrentcodeoutputsasmanycharactersaswillfitonasinglelineofaconsoleapplication;
considermakingeveryimagechosenfitinthesameamountofspacetomaximizetheviewablearea.
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:
o"
case2:
O"
case3:
@"
\n"
Currentlimits:
realCoord:
from{0}to{1}"
realMin,realMax);
imagCoord:
imagMin,imagMax);
Enternewlimits:
from:
realMin=Convert.ToDouble(Console.ReadLine());
to:
realMax=Convert.ToDouble(Console.ReadLine());
imagMin=Convert.ToDouble(Console.ReadLine());
imagMax=Convert.ToDouble(Console.ReadLine());
Chapter5:
MoreAboutVariables
Q.Whichofthefollowingconversionscan’tbeperformedimplicitly:
a.inttoshort
b.shorttoint
c.booltostring
d.bytetofloat
A.Conversionsaandccan’tbeperformedimplicitly.
Q.Givethecodeforacolorenumerationbasedontheshorttypecontainingthecolorsoftherainbowplusblackandwhite.Canthisenumerationbebasedonthebytetype?
enumcolor:
short
Red,Orange,Yellow,Green,Blue,Indigo,Violet,Black,White
Yes,becausethebytetypecanholdnumbersbetween0and255,sobyte-basedenumerationscanhold256entrieswithindividualvalues,ormoreifduplicatevaluesareusedforentries.
Q.ModifytheMandelbrotsetgeneratorexamplefromthelastchaptertousethefollowingstructforcomplexnumbers:
structimagNum
publicdoublereal,imag;
imagNumcoord,temp;
doublerealTemp2,arg;
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)
temp.real=coord.real;
temp.imag=coord.imag;
arg=(coord.real*coord.real)+(coord.imag*coord.imag);
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);
Q.Willthefollowingcodecompile?
string[]blab=newstring[5]
string[5]=5thstring.
A.No,forthefollowingreasons:
*Endofstatementsemicolonsaremissing.
*2ndlineattemptstoaccessanon-existent6thelementofblab.
*2ndlineattemptstoassignastringthatisn’tenclosedindoublequotes.
Q.Writeaconsoleapplicationthatacceptsastringfromtheuserandoutputsastringwiththecharactersinreverseorder.
Enterastring:
stringmyString=Console.ReadLine();
stringreversedString="
;
for(intindex=myString.Length-1;
index>
=0;
index--)
reversedString+=myString[index];
Reversed:
{0}"
reversedString);
Exercise6
Q.Writeaconsoleapplicationthatacceptsastringandreplacesalloccurrencesofthestringnowithyes.
myString=myString.Replace("
no"
"
yes"
Replaced\"
no\"
with\"
yes\"
:
myString);
Exercise7
Q.WriteaconsoleapplicationthatplacesdoubleQuotesaroundeachwordinastring.
myString="
\"
+myString.Replace("
"
\"
)+"
Addeddoublequotesareoundwords:
OrusingString.Split():
string[]myWords=myString.Split('
'
Addingdoublequotesareoundwords:
foreach(stringmyWordinmyWords)
{0}\"
myWord);
Chapter6:
Functions
Q.Thefollowi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 入门 经典 解题 答案