Notes for problem finding and function implementation, and notes for finding implementation

Source: Internet
Author: User

Notes for problem finding and function implementation, and notes for finding implementation

 

Before writing this article, I hesitated for a long time to write questions about the application of arrays and collections, and analyzed the difficulties. However, I thought that the current array and set are only simple applications, and the problems I encountered are only caused by poor basic knowledge and poor introduction of other knowledge. I will analyze and study them carefully, there is always a solution, and there are no suitable examples for analysis. Therefore, this problem is still on hold.

I think back to my experience in C # learning over the past week, and I think I can make a simple thought and summary of the problems encountered during problem searching and program function implementation. On the one hand, I feel that there are difficulties in searching for simple problems, mainly because the code implementation function is not good enough. On the other hand, when I write code, the running result does not implement the desired function. During careful check, it is found that the problem is not fully considered, and the description of the implementation function is incomplete, during encoding, only the implementation of the description function is considered, and the impact of different user conditions is not considered.

If the problem is caused by Incorrect code selection, compilation does not cause errors, but the program running does not achieve the desired effect. To address this type of problem, you can only perform operations from top to bottom on the code that implements the function by referring to the source code based on the error performance. (in this case, you should perform operations on the Statement Analysis by yourself ), to determine the cause of the problem. If it is difficult to judge this problem, we should strengthen the accumulation of basic knowledge, make full memories of the learned knowledge first, and then integrate them. In this case, we should select the most suitable code, in this way, there is sufficient cost to solve the analysis problem. Below are several small examples:

Output in the console:

Make up the following idiom:

For tigers ()

It is very easy to implement this function. However, some people may write as follows:

Console. Write ("make up the following idiom:"); Console. WriteLine ("for Tiger ()"); Console. ReadLine ();

 

Now we can see that this Code does not have line breaks at a glance, but it only uses the Console. write (); statement, without using a line feed statement, or using a line feed escape character. However, in actual situations, it is indeed difficult to find the cause in the face of this phenomenon, it is still necessary to enhance the understanding and accumulation of code. In addition, this problem is deepened. For example, when an input error is returned, an error is prompted and the screen is displayed, waiting for the user to re-enter the problem, or more complex situations such as using longer text and line feed escape characters, can you determine the cause of a problem at a glance? At this time, you can only exclude the part of the Code that implements the function one by one.

Here is another example. The problem occurs not in the statement:

Check whether the integer in the array is a prime number in the console. If it is a prime number, output it: (skip the previous Code and write only the implementation function)

for(i = 0;i < nums.Length; i++)  {
int z = 0; for(j = 1;j < nums[i]; j++) { if(nums[i] % j == 0) { z++; } } if(z==2) Console.Write(nums[i]+"\t"); }Console.ReadLine();

 

After running the program, it is found that there is no prime number in the console. If you try multiple times, one may appear, but one is not a prime number. So where is the problem? According to the preceding method, check the statements first and find that the output result statement is not affected. So again, because the output of the computing result is affected, in addition to the output statement, you also need to consider whether the code implementation and calculation method may be wrong. For this piece of code, consider the calculation method to analyze the code one by one. First, the first for retrieves the numbers in the array one by one, and then uses the second for loop to extract the number from 1 to the number itself to determine whether the number can be divisible. Observe the code and find the determination of the for loop, when the implementation process of the j <nums [I] statement does not meet the requirements of the Self-incrementing Number of j, therefore, no computing data is fully divided by itself during each calculation. To implement this code, change j <nums [I] to j <= nums [I].

It can be seen that a small symbol can cause program function failure, and correct data calculation is also one of the key.

In addition, there is another problem that is not so straightforward. Of course, for convenience, the example is the one above, so you can directly find the problem when you see this Code:

int z = 0;for(i = 0;i < nums.Length; i++)  {    for(j = 1;j <= nums[i]; j++)      {          if(nums[i] % j == 0)           {                z++;           }    if(z==2)       Console.Write(nums[i]+"\t");      }  }Console.ReadLine();

Compared with the above, we can easily see that the value of z cannot be placed outside the first for loop. Otherwise, the value of z is always increased for each loop and has no purpose of verification. Then let's take a look at whether the second if statement should be placed outside the second for loop. Through analysis, we can find that the second if statement does not affect the implementation of program functions. In fact, this example is not very good. Here I want to express the impact of the correct placement of if in the loop on the Implementation of program functions. This situation is generally encountered, the best way is to draw a flowchart to determine the relationship between loops and loops, loops, and locations of judgments, whether they should be included, or tied.

There is also an easy-to-happen error, that is, the use of the scope of the variable, still using an example:

int i;if(true){      int z = 10;      i = z;}Console.Write(i);Console.ReadLine();

Due to its own knowledge reserves, it is impossible to give better examples (we may have encountered it, but there is no record, and we will improve it in the future and summarize and publish it ). Of course, for this piece of code, it is easy to find errors in VS, and you cannot use the unassigned variable I. So I = 0, recycling, will output 0 or 10 at last? After running the code, it is found that the number displayed on the console is 10. After a variable is defined, the memory space of the variable has been generated. The subsequent calculation only changes the stored value and does not change the location of the memory space. In the above case, the output I statement does not judge the scope of the variable, because the definition of I is actually outside of the if function, even if only I = 10 after the if judgment; this statement ignores braces, and the impact of the variable scope still exists. Of course, in actual use, simple code like this can easily avoid errors, but it is inevitable that more complex situations will occur, just reminding you to pay attention to it.

Finally, you have to mention the user input problem. Currently, only program functions are implemented in the console, while mathematical computing is quite common. Currently, we only have access to the basic knowledge of data, budget, loop, array, and set. In the face of situations where users need to input numbers to complete various operations and output the calculation results, you cannot avoid requiring the user to enter the input value. Enter Console. ReadLine () in the Console. The statement obtains data in string format. to convert the data to the numeric type, you must enter at least the decimal data. However, after all, users have their own subjective ideas. Computers cannot limit the specific content of user input. If a string of English words is entered, the computer may cause operation errors. If the number is limited to a few, you can use the input = "Limit number" method to implement conditional control, so that the program runs without a crash. However, the user input is often an uncertain value. In this case, you need to add prompt information to meet the personalized design of the program.

However, in terms of the current situation, it is difficult to find a case where friendly prompts are given in each operation step. In this case, programmers are needed to improve the project's shortcomings. For example, if you are prompted to enter a number or the number entered exceeds the range, you must enter a new number! The number does not exist in the prompt set. Please enter it again to delete it! And so on. The program eventually serves humans. Although no one mentions these things for me at present, personally, the prompt information is like a code comment for programmers, it is also an indispensable part of the key and must be considered to improve the work.

All in all, in the face of programming, we need to consider the implementation of the Code and the effect after implementation from multiple aspects, and import the data for multiple debugging, use Data of multiple types and ranges to determine whether the program can be optimized.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.