c#入门经典详解.docx

上传人:文*** 文档编号:68225869 上传时间:2022-12-27 格式:DOCX 页数:71 大小:107.86KB
返回 下载 相关 举报
c#入门经典详解.docx_第1页
第1页 / 共71页
c#入门经典详解.docx_第2页
第2页 / 共71页
点击查看更多>>
资源描述

《c#入门经典详解.docx》由会员分享,可在线阅读,更多相关《c#入门经典详解.docx(71页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、问题解答Chapter 1: Introducing C#No exercises.Chapter 2: Writing a C# ProgramNo exercises.Chapter 3: Variables and ExpressionsExercise 1Q. In the following code, how would you refer to the name great from code in the namespace fabulous?namespace fabulous(/ code in fabulous namespacenamespace super(names

2、pace smashing(/ great name definedA.super.smashing.greatExercise 2Q. Which of the following is not a legal variable name?a. myVariablelsGoodb. 99Flakec. _f loordtime2GetJiggyWidlteA.b. Because it starts with a number, and,e Because it contains a full stop.Exercise 3Q.Is the string *supercalif ragili

3、sticexpialidocious too big to fit in a stringvariable? Why?A. No, there is no theoretical limit to the size of a string that may be contained in a string variable.Exercise 4Q. By considering operator precedence, list the steps involved in the computation of the following expression:resultVar += varl

4、 * var2 + var3 % var4 / var5;A. The * and / operators have the highest precedence here, followed by +, %, and finally +=. The precedence in the exercise can be illustrated using parentheses as follows:resultVar += (varl * var2) + var3) % (var4 / var5);Exercise 5Q. Write a console application that ob

5、tains four int values from the user and displays their product.A.static void Main(string args) (int firstNumber, secondNumber, thirdNumber, fourthNumber;Console.WriteLine(Give me a number:);firstNumber = Convert.Tolnt32(Console.ReadLine();Console.WriteLine(Give me another number:);secondNumber = Con

6、vert.ToInt32(Console.ReadLine();Console.WriteLine(Give me another number:);thirdNumber = Convert.Tolnt32(Console.ReadLine();Console.WriteLine(Give me another number:);fourthNumber = Convert.Tolnt32(Console.ReadLine();Console.WriteLine(The product of 0, 1, 2, and 3 is 4. firstNumber, secondNumber, th

7、irdNumber, fourthNumber, firstNumber * secondNumber * thirdNumber * fourthNumber);Note that Convert. Tolnt32 () is used here, which isnt covered in the chapter.Chapter 4: Flow ControlExercise 1Q. If you have two integers stored in variables varl and var2, what Boolean test can you perform to see if

8、one or the other (but not both) is greater than 10?A. (varl 10)人(var2 10)Exercise 2Q. Write an application that includes the logic from Exercise 1, obtains two numbers from the user, and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers.A.bool n

9、umbersOK = false;double varl, var2;varl = 0;var2 = 0;while (InumbersOK) (Console.WriteLine(Give me a number:);varl = Convert.ToDouble(Console.ReadLine();Console.WriteLine(Give me another number:);var2 = Convert.ToDouble(Console.ReadLine(); if (varl 10) A (var2 10) (numbersOK = true; else (if (varl =

10、 10) & (var2 10) & (var2 10) (Console.WriteLine(Only one number may be greater than 10.); else (numbersOK = true;)Console.WriteLine(You entered 0 and 1., varl, var2);Exercise 3). What is wrong with the following code?int i;for (i = 1; i = 10; i+) (if ( (i % 2) = 0) continue;Console.WriteLine(i);A. T

11、he code should read:int i;for (i = 1; i = 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 = -1.2; coord.ima

12、g -= 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 = 0; index) (reve rsedSt ring += myStringindex;Console.WriteLine(Reversed: 0

13、, reversedString); Exercise 6Q. Write a console application that accepts a string and replaces all occurrences of the string no with yes.A.static void Main(string args) (Console.WriteLine(Enter a string:);string myString = Console.ReadLine();myString = myString.Replace(no, yes);Console.WriteLine(Rep

14、laced no with yes: 0, myString);1Exercise 7Q. Write a console application that places double Quotes around each word in a string.Console.WriteLine(Enter a string:);string myString = Console.ReadLine();myString =+ myString.Replace( , n MM) + Mn;Console.WriteLine(Added double quotes areound words: 0,

15、myString); Or using String. Split ():static void Main(string args) Console.WriteLine(Enter a string:);string myString = Console.ReadLine();string myWords = myString.Split();Console.WriteLine(Adding double quotes areound words:);foreach (string myWord in myWords)(Console.Write(0 , myWord);Chapter 6:

16、FunctionsExercise 1Q. The following two functions have errors. What are they?static bool Write() (Console.WriteLine(Text output from function.); static void myFunction(string label, params int args, bool showLabel) (if (showLabel)Console.WriteLine(label);foreach (int i in args)Console.WriteLine(0),

17、i); A. The first function has a return type of bool, but doesnt return a bool value.The second function has aparams argument, but it isnt at the end of the argument list.Exercise 2Q. Write an application that uses two command line arguments to place values into a string and an integer variable respe

18、ctively, then display these values.A.static void Main(string args) (if (args.Length != 2) (Console.WriteLine(Two arguments required.); return;string parami = args0;int param2 = Convert.Tolnt32(args1);Console.WriteLine(String parameter: 0, paraml);Console.WriteLine(Integer parameter: 0, param2); Note

19、 that this answer contains code that checks that 2 arguments have been supplied, which wasnt part of the Question but seems logical in this situation.Exercise 3R. Create a delegate and use it to impersonate the Console .ReadLine () function when asking for user input.A.class Program ( delegate strin

20、g ReadLineDelegate();static void Main(string args) (ReadLineDelegate readLine = new ReadLineDelegate(Console.ReadLine);Console.WriteLine(Type a string:);string userinput = readLine();Console.WriteLine(You typed: (0), userInput);Exercise 4S. Modify the following struct to include a function that retu

21、rns the total price of an order struct order public string itemName;public int unitcount;public double unitCost;struct order (public string itemName;public int unitCount;public double unitCost;public double TotalCost() (return unitCount * unitCost; Exercise 5T. Add another function to the order stru

22、ct that returns a formatted string as follows, where italic entries enclosed in angle brackets are replaced by appropriate values:Order Information: items at $ each, total cost $struct order (public string itemName;public int unitCount;public double unitcost;public double TotalCost() (return unitCou

23、nt * unitCost;public string Info() (return Order information: + unitCount.ToString() + ” + itemName + items at $ + unitcost.ToString() + each, total cost $ +TotalCost () .ToStringO;Chapter 7: Debugging and Error HandlingExercise 1Q. kiUsing Trace . Wr iteLine () is preferable to using Debug. WriteLi

24、ne () because the Debug version only works in debug builds: Do you agree with this statement? Why?A. This statement is only true for information that you want to make available in all builds. More often, you will want debugging information to be written out only when debug builds are used. In this s

25、ituation, the Debug. WriteLine () version is preferable.Using the Debug .WriteLine () version also has the advantage that it will not be compiled into release builds, thus reducing the size of the resultant code.Exercise 2Q. Provide code fbr a simple application containing a loop that generates an e

26、rror after 5000 cycles. Use a breakpoint to enter Break mode just before the error is caused on the 5000th cycle. (Note: a simple way to generate an error is to attempt to access a nonexistent array element, such as myArray 1000 in an array with a hundred elements.)A.static void Main(string args) (f

27、or (int i = 1; i 10000; i+)(Console.WriteLine(Loop cycle 0, i);if (i = 5000) Console.WriteLine(args999);In VS, a breakpoint could be placed on the following line:Console.WriteLine(Loop cycle 0,i);The properties of the breakpoint should be modified such that the hit count criterion is “break when hit

28、 count is equal to 500.In VCE, a breakpoint could be placed on the line that causes the error because you cannot modify the properties of breakpoints in VCE in this way.Exercise 3Q. ”finally code blocks execute only if a catch block isnt executed.True or false?A. False, finally blocks always execute

29、. This may occur after a cat ch block has been processed.Exercise 4Q. Given the enumeration data type orientat ion defined in the following code, write an application that uses Structured Exception Handling (SEH) to cast a byte type variable into an orientation type variable in a safe way. (Note: yo

30、u can force exceptions to be thrown using the checked keyword, an example of which is shown here. This code should be used in your application.) enum orientation : byte (north = 1,south = 2, east = 3, west = 4 myDirection = checked (orientation)myByte);A,static void Main(string args) (orientation my

31、Direction;for (byte myByte = 2; myByte 10; myByte+) (trymyDirection = checked(orientation)myByte); if (myDirection orientation.west)throw new ArgumentOutOfRangeException(myByte, myByte, Value must be between 1 and 4);)catch (ArgumentOut0fRangeException e) (/ If this section is reached then myByte 4.Console.WriteLine(e.Message);Console.WriteLine(Assigning default value, orientation.north.); myDirection = orientation.north;Console.WriteLine(myDirection = 0, myDirection);This is a bit of a trick question. Because the enumeration is based on the byte type any byte value may be assigned to it, eve

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com