Going into the village -- let's talk about the program development process and program development in the village

Source: Internet
Author: User

Going into the village -- let's talk about the program development process and program development in the village

Finally, you can write a bit of code. Next, we will use a few small examples to learn java. Before that, let's talk about the steps to write programs. Everyone has their own development habits. This is not a standard step, so here is just a reference for me.

Follow the Convention to see the content of this time:

 

1. Steps for writing a program

When I first got involved in programming the world, I practiced grammar and then wrote a xx management system. I thought about what to write and there was no way to understand it. But to become a good programmer, you must have a complete set of program development processes, which will allow you to develop projects faster and clearer.

Every class is written according to the "pseudo code --> test code --> Target Code". This is a good method. No one knows who is using it, and you can never stop using it.

2. Why do I need to write test code first?

This idea comes from Extreme Programming, Extreme Programming (XP for short). This can make Programming faster and simpler, and I feel that it is OK. By testing the code, you know what kind of functions the class method needs to complete is considered qualified. This is very simple. You only need to let the class you write pass the test code, when there will be better technologies to implement the current functions in the future, you will not be afraid to finish modifying the code, there will be bugs, you just need to pass your original test, the so-called unit test seems like this.

 

3. A small example practices the above programming ideas

Why not to mention XP? This is not something that can be done at the moment. You can check your own information.

To write a DotCom game, of course, there is no expectation for any graphic interface. This is the case for beginners. I used to do the same thing. In the future, there will be graphic interface programs. Java is a back-end language that organizes the logical relationship of a project. Therefore, you should practice logic more. Do not think about using pure java as a beautiful interface.

This is a game that attacks the dashboard network. It feels so high. However, it is a small game to guess numbers. There are three dashboard companies on the 7x7 lattice, each of which occupies three consecutive grids. Their seats are randomly generated. You can guess (A0, or F5) to determine whether you hit the target. If you do not guess, the result is "miss", or "hit ", if you guess the three coordinates of a DotCom, the result is "kill ".

 

 

 

This grid does not exist, you know, I know, players know (I don't know if anyone is playing ). But are we just starting to write a really meaningful program? So we reduce the difficulty and convert 2D to one dimension. It looks like the following.

Step by step:

What is the main class of this program? Of course this is DotCom.

What are its main functions? Is the target of a hit, has its own coordinates, and checks whether it is hit.

So its attribute is: the int [] localCells array that stores the information of the grid, and the attribute that records the number of hits on itself, int numOfHits. Method: The checkYourself () method is used to check whether a player is hit. It has parameters to accept the player's guess value and returns results. Therefore, String checkYourself (int userGuess ).

Summarize the main attributes and methods:

  • Attributes: int [] lockaCells; int numOfHits;
  • Methods: String checkYourself (int userGuess ).

Of course, there are other methods, such as property encapsulation methods, setter (), getter (), which are not brain methods, so we will not mention them.

Write the pseudo code of the main method:

1 // It is not a regular pseudo code. just clarify the process. 2 checkYourself (userGuess): 3 result = "miss" 4 for (I: localCells) // loop, judge whether userGuess is equal to the value 5 in localCells if userGuess = I 6 numOfHits + = 1 7 result = "hit" 8 break 9 if numOfHits = 310 result = "kill" 11 return result

 

Well, write real codes, but test codes firstly.

1 public class Test {2 public static void main (String [] args) {3 SimpleDotCom dotCom = new SimpleDotGom (); 4 int [] localCells = {2, 3, 4 }; // specifies the location of DotCom. In real games, it is a randomly generated 5 dotCom. setLocalCells (localCells); 6 int guess = 2; // number 7 String result = dotCom. checkYourself (guess); // Test Method 8 System. out. println ("result =" + result); 9} 10}

 

Well, write SimpleDotCom:

1 public class SimpleDotCom {2 private int [] localCells; 3 private int numOfHits; 4 public void setLocalCells (int [] localCells) {5 this. localCells = localCells; 6} 7 public String checkYourself (int userGuess) {8 String result = "miss"; 9 for (int I: localCells) {10 if (I = userGuess) {11 result = "hit"; 12 numOfHits ++; 13 break; 14} 15} 16 if (3 = numOfHits) {// This is a small detail of a programming specification, 17 result = "kill"; 18} 19 return result; 20} 21}

 

Result:

As expected, we are here today. In fact, this program has a lot of bugs. You can try to find and change it. Next time, write a better one.

 

Let's talk about details.

We all know that a number is a parity method. The two most commonly used methods are num % 2 = 0 or num % 2 = 1. Maybe you may be confused. Is this different? Yes?

The results are different:

But python does not have this problem:

This is determined by the language features. Therefore, we will use num % 2 = 0 to determine whether to use num % 2 = 1 in the future. This is the power of detail.

Daily sentence:

Life doesn't just happen for you; you receive everything in your life based on what you 've got given.

Everything you have today comes from yesterday, and everything you have tomorrow depends on today's performance.

 

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.