Recently learning Python, which has this problem
Classical question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month.
Did not make out, first with a pen in the paper when it is also a blur. Later to find a way to modeling operations, after all, the Java language known as simulating nature, has always been a solution, the process encountered a lot of problems, but also some thinking, now I recognize the content to share to everyone, I hope to help
First, decompose the problem, the big problem transforms to the small problem, the small problem solves by the programming method.
Second, write a comment and build your own class according to the contents of the annotation.
Okay, here's the code.
First Class Helo.java
Import java.util.ArrayList;
/**
* Created by y_a on 2016/12/8.
* * Public
class Helo {public
static void Main (string[] args) {
Tuzi hehe = new Tuzi (0);
arraylist<tuzi> Tuziwo = new arraylist<tuzi> ();
Tuziwo.add (hehe);
for (int i = 1; i < i++) {
//rabbit with age greater than 3 for
(int j = 0; J < Tuziwo.size (); j + +) {
if tuziwo.get (j). Isagebigger (i)) {
//Add the bunny to the Nest
Tuziwo.add (new Tuzi (i));
}} System.out.print (Tuziwo.size ());
}
Second Class Tuzi.java
/**
* Created by y_a on 2016/12/9.
* * Public
class Tuzi {
int birthday = 0;
public Tuzi (int birthday) {
this.birthday = birthday;
}
public boolean isagebigger (int currentday) {return
(Currentday-birthday >= 2)? True:false;
}
Result diagram