软工班级课件_第1页
软工班级课件_第2页
软工班级课件_第3页
软工班级课件_第4页
软工班级课件_第5页
已阅读5页,还剩86页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 2009 Pearson Education, Inc. All rights reserved.13Introduction toC# Applications 2009 Pearson Education, Inc. All rights reserved.2OBJECTIVESIn this chapter you will learn: To write simple C# applications using code ratherthan visual programming. Input data from the keyboard and output data to the

2、 screen. To declare and use data of various types. To store and retrieve data from memory. To use arithmetic operators. To determine the order in which operators are applied. To write decision-making statements. To use relational and equality operators. 2009 Pearson Education, Inc. All rights reserv

3、ed.33.1 Introduction3.2 A Simple C# Application: Displaying a Line of Text3.3 Creating a Simple Application in Visual Studio3.4 Modifying Your Simple C# App3.5 Formatting Text with Console.WriteConsole.Write and Console.WriteLineConsole.WriteLine3.6 Another C# Application: Adding Integers3.7 Memory

4、Concepts3.8 Arithmetic3.9 Decision Making: Equality and Relational Operators3.10 (Optional) Examining the ATM Requirements Document 2009 Pearson Education, Inc. All rights reserved.43.1 Introduction Console applications input and output text in a console window, which in Windows XP and Windows Vista

5、 is known as the Command Prompt. 2009 Pearson Education, Inc. All rights reserved.5 Figure 3.1 shows a simple application that displaysa line of text.OutlineWelcome1.csWelcome1.csFig. 3.1 | Text-displaying application. Class declaration for class Welcome1.Comments improve code readability. 2009 Pear

6、son Education, Inc. All rights reserved.63.2 A Simple C# Application: Displaying a Line of Text (Cont.) Programmers insert comments to document applications. Comments improve code readability. The C# compiler ignores comments, so they do not cause the computer to perform any action when the applicat

7、ion is run. A comment that begins with / is called a single-line comment, because it terminates at the end of the line on which it appears. A / comment also can begin in the middle of a line and continue until the end of that line. Delimited comments begin with the delimiter /* and end with the deli

8、miter */. All text between the delimiters is ignored by the compiler. 2009 Pearson Education, Inc. All rights reserved.73.2 A Simple C# Application: Displaying a Line of Text (Cont.) Common Programming Error 3.1Forgetting one of the delimiters of a delimited comment is a syntax error. The syntax of

9、a programming language specifies the rules for creating a proper application in that language. A syntax error occurs when the compiler encounters code that violates C#s language rules. 2009 Pearson Education, Inc. All rights reserved.83.2 A Simple C# Application: Displaying a Line of Text (Cont.) A

10、usingusing directive tells the compiler where to look for a predefined class that is used in an application. Predefined classes are organized under namespacesnamed collections of related classes. Collectively, .NETs namespaces are referred to as the .NET Framework Class Library. The System namespace

11、 contains the predefined Console class and many other useful classes. 2009 Pearson Education, Inc. All rights reserved.93.2 A Simple C# Application: Displaying a Line of Text (Cont.) Error-Prevention Tip 3.1Forgetting to include a usingusing directive for a namespace that contains a class used in yo

12、ur application typically results in a compilation error, containing a message such as “The name The name Console does not exist in the current Console does not exist in the current contextcontext.” When this occurs, check that you provided the proper usingusing directives and that the names in the u

13、sing using directives are spelled correctly, including proper use of Uppercase and lowercase letters. 2009 Pearson Education, Inc. All rights reserved.103.2 A Simple C# Application: Displaying a Line of Text (Cont.) Programmers use blank lines and space characters to make applications easier to read

14、. Together, blank lines, space characters and tab characters are known as whitespace. Whitespace is ignored by the compiler. 2009 Pearson Education, Inc. All rights reserved.113.2 A Simple C# Application: Displaying a Line of Text (Cont.) Keywords (sometimes called reserved words) are reserved for u

15、se by C# and are always spelled with all lowercase letters. Every application consists of at least one class declaration that is defined by the programmer. These are known as user-defined classes. The classclass keyword introduces a class declaration and is immediately followed by the class name. 20

16、09 Pearson Education, Inc. All rights reserved.123.2 A Simple C# Application: Displaying a Line of Text (Cont.) Good Programming Practice 3.1By convention, always begin a class names identifier with a capital letter and start each subsequent word in the identifier with a capital letter. 2009 Pearson

17、 Education, Inc. All rights reserved.13Fig. 3.2 | C# keywords and contextual keywords. (Part 1 of 2.) A class name is an identifier: Series of characters consisting of letters, digits and underscores ( _ ). Cannot begin with a digit and does not contain spaces. The complete list of C# keywords is sh

18、own in Fig. 3.2. 3.2 A Simple C# Application: Displaying a Line of Text (Cont.) 2009 Pearson Education, Inc. All rights reserved.14Fig. 3.2 | C# keywords and contextual keywords. (Part 2 of 2.) The contextual keywords in Fig. 3.2 can be used as identifiersoutside the contexts in which they are keywo

19、rds, but for claritythis is not recommended. 3.2 A Simple C# Application: Displaying a Line of Text (Cont.) 2009 Pearson Education, Inc. All rights reserved.153.2 A Simple C# Application: Displaying a Line of Text (Cont.) C# is case sensitivethat is, uppercase and lowercase letters are distinct, so

20、a1 and A1 are different (but both valid) identifiers. Common Programming Error 3.2C# is case sensitive. Not using the proper uppercase and lowercase letters for an identifier normally causes a compilation error. Identifiers may also be preceded by the character. This indicates that a word should be

21、interpreted as an identifier, even if it is a keyword (e.g. int). 2009 Pearson Education, Inc. All rights reserved.163.2 A Simple C# Application: Displaying a Line of Text (Cont.) Good Programming Practice 3.2By convention, a file that contains a single publicpublic class should have a name that is

22、identical to the class name (plus the .cs .cs extension) in both spelling and capitalization. Naming your files in this way makes it easier for other programmers (and you) to determine where the classes of an application are located. 2009 Pearson Education, Inc. All rights reserved.173.2 A Simple C#

23、 Application: Displaying a Line of Text (Cont.) A left brace, , begins the body of every class declaration. A corresponding right brace, , must end each class declaration. Error-Prevention Tip 3.2Whenever you type an opening left brace, , in your application, imeediately type the closing right brace

24、, , then reposition the cursor between the braces and indent to begin typing the body. This practice helps prevent errors due to missing braces.Good Programming Practice 3.3Indent the entire body of each class declaration one “level” of indentation between the left and right braces that delimit the

25、body of the class. This format emphasizes the class declarations structure and makes it easier to read. You can let the IDE format your code by selecting Edit Advanced Format Document. 2009 Pearson Education, Inc. All rights reserved.183.2 A Simple C# Application: Displaying a Line of Text (Cont.) G

26、ood Programming Practice 3.4Set a convention for the indent size you prefer, then uniformly apply that convention. The Tab key may be used to create indents, but tab stops vary among text editors. We recommend using three spaces to form each level of indentation. We show how to do this in Section 3.

27、3.Common Programming Error 3.3It is a syntax error if braces do not occur in matching pairs. 2009 Pearson Education, Inc. All rights reserved.193.2 A Simple C# Application: Displaying a Line of Text (Cont.) Parentheses after an identifier indicate that it is an application building block called a me

28、thod. Class declarations normally contain one or more methods. Method names usually follow the same casing capitalization conventions used for class names. For each application, one of the methods in a class must be called Main; otherwise, the application will not execute. Methods are able to perfor

29、m tasks and return information when they complete their tasks. Keyword voidvoid indicates that this method will not return any information after it completes its task. 2009 Pearson Education, Inc. All rights reserved.203.2 A Simple C# Application: Displaying a Line of Text (Cont.) The body of a meth

30、od declaration begins with a left brace and ends with a corresponding right brace. Good Programming Practice 3.5As with class declarations, indent the entire body of each method declaration one “level” of indentation between the left and right Braces that define the method body. This format makes th

31、e structure of the method stand out and makes the method declaration easier to read. 2009 Pearson Education, Inc. All rights reserved.213.2 A Simple C# Application: Displaying a Line of Text (Cont.) Characters between double quotation marks are strings. Whitespace characters in strings are not ignor

32、ed by the compiler. The Console.WriteLineConsole.WriteLine method displays a line of text in the console window. The string in parentheses is the argument to the Console.WriteLineConsole.WriteLine method. Method Console.WriteLineConsole.WriteLine performs its task by displaying (also called outputti

33、ng) its argument in the console window. 2009 Pearson Education, Inc. All rights reserved.223.2 A Simple C# Application: Displaying a Line of Text (Cont.) A method is typically composed of one or more statements that perform the methods task. Most statements end with a semicolon. Common Programming E

34、rror 3.4Omitting the semicolon at the end of a statement is a syntax error. 2009 Pearson Education, Inc. All rights reserved.233.2 A Simple C# Application: Displaying a Line of Text (Cont.) Error-Prevention Tip 3.3When the compiler reports a syntax error, the error may not be in the line indicated b

35、y the error message. First, check the line for which the error was reported. If that line does not contain syntax errors, check several preceding lines.Good Programming Practice 3.6Following the closing right brace of a method body or class declaration with a comment indicating the method or class d

36、eclaration to which the brace belongs improves application readability. 2009 Pearson Education, Inc. All rights reserved.243.3 Creating a Simple Application in Visual C# ExpressCreating the Console Application Select File New Project to display the New Project dialog (Fig. 3.3). Select the Console A

37、pplication template. In the dialogs Name field, type Welcome1, and click OK to create the project. 2009 Pearson Education, Inc. All rights reserved.253.3 Creating a Simple Application in Visual C# Express (Cont.)Project nameFig. 3.3 | Creating a Console Application with the New Project dialog. proje

38、ct name 2009 Pearson Education, Inc. All rights reserved.263.3 Creating a Simple Application in Visual C# Express (Cont.) The IDE now contains the open console application. The code coloring scheme used by the IDE is called syntax-color shading and helps you visually differentiate application elemen

39、ts. 2009 Pearson Education, Inc. All rights reserved.273.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.4 | IDE with an open console application. Editor window 2009 Pearson Education, Inc. All rights reserved.283.3 Creating a Simple Application in Visual C# Express (Cont.) To hav

40、e the IDE display line numbers, select Tools Options. In the dialog that appears (Fig. 3.5), click the Show all settings checkbox on the lower left of the dialog. Expand the Text Editor node in the left pane and select All Languages. On the right, check the Line numbers checkbox. 2009 Pearson Educat

41、ion, Inc. All rights reserved.293.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.5 | Modifying the IDE settings. 2009 Pearson Education, Inc. All rights reserved.303.3 Creating a Simple Application in Visual C# Express (Cont.) To set code indentation to three spaces per indent: I

42、n the Options dialog that you opened in the previous step, expand the C# node in the left pane and select Tabs. Make sure that the option Insert spaces is selected. Enter 3 for both the Tab size and Indent size fields. Click OK to save your settings, close the dialog and return to the editor window.

43、 2009 Pearson Education, Inc. All rights reserved.313.3 Creating a Simple Application in Visual C# Express (Cont.) To rename the application file, click Program.cs in the Solution Explorer window to display its properties in the Properties window (Fig. 3.6). Change the File Name property to Welcome1

44、.cs. 2009 Pearson Education, Inc. All rights reserved.323.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.6 | Renaming the program file in the Properties window. Click Program.cs todisplay its propertiesType Welcome1.cs hereto rename the fileFile Name propertyProperties windowSolu

45、tion Explorer 2009 Pearson Education, Inc. All rights reserved.333.3 Creating a Simple Application in Visual C# Express (Cont.) IntelliSense lists a classs members, which include method names. As you type characters, Visual C# Express highlights the first member that matches all the characters typed

46、, then displays a tool tip containing a description of that member. You can either type the complete member name, double click the member name in the member list or press the Tab key to complete the name. While the IntelliSense window is displayed pressing the Ctrl key makes the window transparent s

47、o you can see the code behind the window. 2009 Pearson Education, Inc. All rights reserved.343.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.7 | IntelliSense feature of Visual C# Express. Partially typed nameIntelliSense windowClosest match is highlightedTool tip describes highl

48、ighted item 2009 Pearson Education, Inc. All rights reserved.353.3 Creating a Simple Application in Visual C# Express (Cont.) When you type the open parenthesis character, (, after a method name, the Parameter Info window is displayed (Fig. 3.8). This window contains information about the methods pa

49、rameters. Down arrowParameter Info windowUp arrowFig. 3.8 | Parameter Info window. Up and down arrows allow you to scroll through overloaded versions of the method. 2009 Pearson Education, Inc. All rights reserved.363.3 Creating a Simple Application in Visual C# Express (Cont.) To save an applicatio

50、n, select File Save All to display the Save Project dialog. In the Location textbox, specify the directory where you want to save this project. Select the Create directory for solution checkbox and click Save. 2009 Pearson Education, Inc. All rights reserved.373.3 Creating a Simple Application in Vi

51、sual C# Express (Cont.) To compile an application, select Build Build Solution. To execute it, select Debug Start Without Debugging (or type Ctrl + F5). This invokes the Main method. Figure 3.10 shows the results of executing this application, displayed in a console (Command Prompt) window. 2009 Pea

52、rson Education, Inc. All rights reserved.383.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.9 | Executing the application shown in Fig. 3.1. Console window 2009 Pearson Education, Inc. All rights reserved.393.3 Creating a Simple Application in Visual C# Express (Cont.)Error-Preve

53、ntion Tip 3.4When learning how to program, sometimes it is helpful to “break” a working application so you can familiarize yourself with the compilers syntax-error messages. Try removing a semicolon or brace from the code of Fig. 3.1, then recompiling the application to see the error messages genera

54、ted by the omission. 2009 Pearson Education, Inc. All rights reserved.403.3 Creating a Simple Application in Visual C# Express (Cont.) As you type code, the IDE responds either by applying syntax-color highlighting or by generating a syntax error. A syntax error indicates a violation of Visual C#s r

55、ules for creating correct applications. When a syntax error occurs, the IDE underlines the error in red and provides a description of it in the Error List window (Fig. 3.10). 2009 Pearson Education, Inc. All rights reserved.413.3 Creating a Simple Application in Visual C# Express (Cont.)Fig. 3.10 |

56、Syntax errors indicated by the IDE.Intentionally omitted semicolon (syntax error)Squiggly underline indicates a syntax errorError description(s)Error List window 2009 Pearson Education, Inc. All rights reserved.423.3 Creating a Simple Application in Visual C# Express (Cont.)Error-Prevention Tip 3.5O

57、ne syntax error can lead to multiple entries in the Error List window. Each error that you address could eliminate several subsequent error messages when you recompile your application. So when you see an error you know how to fix, correct it and recompilethis may make several other errors disappear

58、. 2009 Pearson Education, Inc. All rights reserved.43 Class Welcome2, shown in Fig. 3.11, uses two statements to produce the same output as that shown in the previous example. Unlike WriteLine, the Console classs Write method does not position the screen cursor at the beginning of the next line in t

59、he console window. 3.4 Modifying Your Simple C# Application 2009 Pearson Education, Inc. All rights reserved.44Fig. 3.11 | Displaying one line of text with multiple statements.The Write method does not move the cursor to a new line after displaying its argument.3.4 Modifying Your Simple C# Applicati

60、on (Cont.)Welcome2.csWelcome2.cs 2009 Pearson Education, Inc. All rights reserved.45 A single statement can display multiple lines by using newline characters. Like space characters and tab characters, newline characters are whitespace characters. The application of Fig. 3.12 outputs four lines of t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论