For loop output tree shape "java"

Source: Internet
Author: User

Use the FOR Loop statement to output the following tree effect:

    *   ***  ***** ****************    *    *    *    *    *

Code:

/*    * * * * * * * * * * * * **************** * * * * Analysis:-Tree Top (total 5 layers) per layer: each layer of space per layer + number of stars per layer 1th-empty Number of squares: 4 + stars: 1 2nd--Spaces: 3 + stars: 3 3rd--Number of spaces: 2 + Stars: 4 level 4--spaces: 1 + stars: 7 5th Layer--Number of spaces: 0 + stars: 9 Analysis: Layer 1th--spaces: 4 (= 5-1) + stars: 1 (= 2*1-1) 2nd Layer--spaces: 3 (= 5-            2) + stars: 3 (= 2*2-1) 3rd Floor--Number of spaces: 2 (= 5-3) + stars: 4 (= 2*3-1) 4th level--Number of spaces: 1 (= 5-4) + stars: 7 (= 2*4-1)  5th Floor--Number of spaces: 0 (= 5-5) + Stars: 9 (= 2*5-1) Reasoning: (Layer tree top) level I--number of spaces = (layer-i) + (number of stars = 2*i-1)-  Trunk (total 5 layers) 1th floor--spaces: 4 + stars: 1 2nd--Spaces: 4 + stars: 1 3rd--Spaces: 4 + stars: 1 level 4-- Number of spaces: 4 + stars: 1 5th--Spaces: 4 + stars: 1 similar to the 1th result, i.e.: layer 1th--spaces: 4 + stars: 1 The result of the first layer of the analogy tree top Draw: 1th Floor--Number of spaces: (LAYER-1) + stars: 1 Analysis: (Layer tree top) 1th--Number of spaces: (LAYER-1) + stars: 1 2nd--Space number: (Lay            ER-1) + number of stars: 13rd Floor--Number of spaces: (LAYER-1) + stars: 1 Layer 4--Number of spaces: (LAYER-1) + stars: 1 5th Floor--Number of spaces: (LAYER-1) + stars: 1 */ Public classstartree{ Public Static voidMain (string[] args) {intLayer = 5; //first, the tree top         for(inti=1;i<=layer;i++){                         //1, to get a space to display the number            intSpacenum = layer-i;  for(intj=1;j<=spacenum;j++) {//the number of spaces that need to be printedSystem.out.print (""); }                        //2. Get the number of stars to show            intStarnum = 2*i-1;  for(intj=1;j<=starnum;j++) {//number of stars to printSystem.out.print ("*"); }            //3. Line breakSystem.out.println (); }                //second, the trunk         for(inti=1;i<=layer;i++){            //1, to get a space to display the number            intSpacenum = Layer-1;  for(intj=1;j<=spacenum;j++) {System.out.print (" "); }            //2. Get the number of stars to display and wrapSystem.out.println ("*"); }    }}

The Code results show:

For loop output tree shape "java"

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.