The Java solution of Rabbit __java

Source: Internet
Author: User

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

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.