Process Control we use it very much in our programming, and in the last chapter we simply introduce each kind of use, and do not apply to the example, it is difficult to understand its real role. Below we will actually use the process Control code to write some instance-related programs, deepen the understanding of the process control, and explain the exception handling and debugging the use of breakpoints.
The first example we use Switch statement to write a everybody in QQ or something you've played in. Enter your name and see what you did in your life. Here's a final result:
Is the above example very similar to what we shared on the phone? Let's think about this process, enter the name - system generated random number- First name + random number output of the text think about it and look at the following code:
The above instantiates a random number class object (the instantiation will come up here in a later chapter that we know how to use OK) instantiate a random number between 1-6 and then use a variable name to accept the name we entered from the console, When the instantiated Rnumber object is used as a parameter in switch, a number is randomly generated from 1-6 when the program is started, and then the corresponding content is executed in the case. Isn't it simple?
Use for Loop Output About multiplication table, final effect
The multiplication table here is the output of a row, the first line loops 2 times, the second line loops 2 times, in turn .... Here you need to use a nested loop, with the outermost loop 9 times, because there are 9 rows, and each row loops its rows several times. Here's a look at the code:
If the judgment here does not demonstrate, in the previous article also has a case, the following is about the program debugging, exception handling of common methods
Breakpoint Debugging: breakpoint debugging is mainly used to control the operation of the program, by means of interruption in the way we think the problem of the code. First we need to set a breakpoint (click on an empty space before the line number) – Step through (F11 or select in the menu bar) – Watch the variable –f10 step-by-process debugging.
First set a breakpoint:
The value of the variable does not change at this time when F11 is pressed to execute the statement.
The value of the STR variable has changed. So we can set a breakpoint to let the program to step through, so it is convenient for us to debug.
Exception Handling: exception Handling in the program is a very important mechanism, when your software program error bugs, you can use exception handling, common exception handling has caught exceptions or throw exceptions, here I do not recommend the use of throwing exceptions, In a system without good to catch the exception and write to the log, and choose to throw a blind eye will produce many unknown errors, it is recommended to catch the exception and log the way to record, convenient for later maintenance of the program.
The above case attempts to assign a string type and assign a Chinese variable to the int type, which is obviously not possible, here will be an exception to capture and output the console, the exception handling format is try{} statement block in the possible exception is placed in the code, CATSH (Exception e) The e parameter holds the information for the exception.
You can also use Catsh (Exceptione) {throw; } throws an exception, but it is generally not recommended.
Basic knowledge of C #-Application of Process Control (iv)